R/class_dictionary.R

Defines functions getValueFromDictionary setValueToDictionary addValueToDictionary initDictionary print.Dictionary as.list.Dictionary as.vector.Dictionary cloneDictionary getDictionarySubset getDictionaryKeyByValue .assertIsDictionary createDictionary

## |
## |  rpact: Confirmatory Adaptive Clinical Trial Design and Analysis
## |  This file is part of the rpact R package.
## |
## |  RPACT — Statistical Tools for Drug Development
## |
## |  Authors: Gernot Wassmer, PhD, and Friedrich Pahlke, PhD
## |  SPDX-FileCopyrightText: RPACT GmbH, Germany <info@rpact.com>
## |  SPDX-License-Identifier: LGPL-3.0-only
## |
## |  Licensed under the GNU Lesser General Public License, version 3.
## |  License text: https://www.gnu.org/licenses/lgpl-3.0.html
## |
## |  Package documentation: https://www.rpact.org
## |  Resources and customer portal: https://connect.rpact.app
## |  Consulting and services: https://www.rpact.com
## |  Contact: info@rpact.com
## |

createDictionary <- function(name, keyValuePairList = NULL) {
    result <- new.env(parent = emptyenv())
    class(result) <- "Dictionary"
    attr(result, "name") <- name
    if (!is.null(keyValuePairList)) {
        initDictionary(result, keyValuePairList)
    }
    return(result)
}

.assertIsDictionary <- function(x) {
    if (is.null(x)) {
        stopIllegalArgument("'x' must be a valid 'Dictionary' (is NULL)",
            functionName = ".assertIsDictionary",
            parameter = "x",
            value = x
        )
    }
    if (!inherits(x, "Dictionary")) {
        stopIllegalArgument("'x' must be an instance of class 'Dictionary' (is ", .getClassName(x), ")",
            functionName = ".assertIsDictionary",
            parameter = "x",
            value = .getClassName(x)
        )
    }
}

#'
#' @examples
#' \dontrun{
#' x <- createDictionary("x", list(a = 1, b = 2, c = 3))
#' getDictionaryKeyByValue(x, 2)
#' }
#'
#' @noRd
#'
getDictionaryKeyByValue <- function(x, value) {
    .assertIsDictionary(x)
    keys <- names(x)
    values <- as.vector(x)
    return(unique(keys[values == value]))
}

#'
#' @examples
#' \dontrun{
#' x <- createDictionary("x", list(a = 1, b = 2, c = 3))
#' getDictionarySubset(x, c("b", "c"))
#' }
#'
#' @noRd
#'
getDictionarySubset <- function(x, subsetNames) {
    .assertIsDictionary(x)
    result <- createDictionary(attr(x, "name"))
    for (objName in base::ls(envir = x)) {
        if (objName %in% subsetNames) {
            base::assign(objName, x[[objName]], envir = result)
        }
    }
    return(result)
}

cloneDictionary <- function(x) {
    .assertIsDictionary(x)
    result <- createDictionary(attr(x, "name"))
    for (objName in base::ls(envir = x)) {
        base::assign(objName, x[[objName]], envir = result)
    }
    return(result)
}

#'
#' @export
#' @noRd
#'
as.vector.Dictionary <- function(x, ...) {
    .assertIsDictionary(x)
    vec <- character()
    for (key in names(x)) {
        vec <- c(vec, base::get(key, envir = x))
    }
    return(vec)
}

#'
#' @export
#' @noRd
#'
as.list.Dictionary <- function(x, ...) {
    .assertIsDictionary(x)
    result <- list()
    for (key in names(x)) {
        result[[key]] <- base::get(key, envir = x)
    }
    return(result)
}

#'
#' @export
#' @noRd
#'
print.Dictionary <- function(x, ...) {
    .assertIsDictionary(x)
    cat(attr(x, "name"), "\n")
    for (key in names(x)) {
        cat("$", key, "\n", sep = "")
        print(base::get(key, envir = x))
        cat("\n")
    }
}

initDictionary <- function(x, keyValuePairList) {
    .assertIsDictionary(x)
    if (is.null(keyValuePairList) || length(keyValuePairList) == 0 || !is.list(keyValuePairList)) {
        stopIllegalArgument("'keyValuePairList' must be a valid list",
            functionName = "initDictionary",
            parameter = "keyValuePairList",
            value = keyValuePairList
        )
    }

    if (any(names(keyValuePairList) == "")) {
        stopIllegalArgument("'keyValuePairList' must be a named list",
            functionName = "initDictionary",
            parameter = "keyValuePairList",
            value = keyValuePairList
        )
    }

    for (key in names(keyValuePairList)) {
        value <- keyValuePairList[[key]]
        addValueToDictionary(x, key, value)
    }
}

addValueToDictionary <- function(x, key, value) {
    .assertIsDictionary(x)
    if (base::exists(key, envir = x)) {
        stopIllegalArgument("dictionary ", base::sQuote(base::attr(x, "name")),
            " already contains key ", base::sQuote(key),
            functionName = "addValueToDictionary"
        )
    }
    setValueToDictionary(x, key, value)
}

setValueToDictionary <- function(x, key, value) {
    .assertIsDictionary(x)
    base::assign(key, value, envir = x)
}

getValueFromDictionary <- function(x, key) {
    .assertIsDictionary(x)
    if (!base::exists(key, envir = x)) {
        stopIllegalArgument("dictionary ", base::sQuote(base::attr(x, "name")),
            " does not contain key ", base::sQuote(key),
            functionName = "getValueFromDictionary"
        )
    }

    return(base::get(key, envir = x))
}

Try the rpact package in your browser

Any scripts or data that you put into this service are public.

rpact documentation built on Nov. 17, 2026, 1:07 a.m.