Create a function that creates an object and assigns it to a namespace the first time it's called and subsequently retrieves it from the namespace thereafter.
create_simple_get_function.Rd
Useful when the object depends on a long running task such as a database query.
Arguments
- obj_nm
chr
Name of the object- env_expr
expr/chr
name of the namespace to assign the object to, or an expression that returns the environment to assign to- call_expr
expr
The code used to construct the object if the object hasn't already been constructed- as_character
Should the function return code as a character? (Default is an expression)
Examples
create_simple_get_function("mt_cars", .GlobalEnv, dplyr::mutate(mtcars, cyl = as.character(cyl)))
#> .Primitive("<-")(get_mt_cars, function (env = .GlobalEnv)
#> {
#> if (exists("mt_cars", envir = env)) {
#> get0("mt_cars", envir = env)
#> }
#> else {
#> dplyr::mutate(mtcars, cyl = as.character(cyl))
#> }
#> })
#> .Primitive("<-")(get_mt_cars, function (env = .GlobalEnv)
#> {
#> if (exists("mt_cars", envir = env)) {
#> get0("mt_cars", envir = env)
#> }
#> else {
#> dplyr::mutate(mtcars, cyl = as.character(cyl))
#> }
#> })