[R-pkg-devel] define an environment in .onLoad

Martin Morgan mtmorg@n@b|oc @end|ng |rom gm@||@com
Tue Jan 29 23:16:31 CET 2019


A variant (not quite as good because parent = emptyenv() is not an argument) is

pkg_data <- local({ a = 1; b =2; environment() })

If information at load time is also important, one might add

.onLoad <- function(libname, pkgname) {
    pkg_data[["last_update"]] <- Sys.time()
}

which is actually a fun test of one's understanding of R's assignment and symbol look-up rules.

Martin Morgan

On 1/29/19, 4:50 PM, "R-package-devel on behalf of Gábor Csárdi" <r-package-devel-bounces using r-project.org on behalf of csardi.gabor using gmail.com> wrote:

    You don't need .onLoad for this, just put the environment into the
    package environment. E.g. simply add
    
    pkg_data <- new.env(parent = emptyenv())
    
    to your package code. Then you can refer to pkg_data from the
    functions in the package.
    
    Best,
    Gabor
    
    On Tue, Jan 29, 2019 at 9:40 PM Pascal Title <pascaltitle using gmail.com> wrote:
    >
    > Hi,
    >
    > I am developing an R package where I would like to have a set of names
    > defined once, and which could then be queried from within the various
    > functions of the package. The way I have currently set this up is to define
    > a new environment that contains these names.
    >
    > So, what I currently have is:
    >
    > .var <- new.env()
    >
    > .var$bio <- "bio_"
    >
    > .var$tmin <- "tmin_"
    >
    > .var$tmax <- "tmax_"
    >
    > What I would like is that this environment be created when the R package is
    > loaded. That way, default values are automatically in place, and if the
    > user would like to change the naming that they use for tmin, for example,
    > they would just need to do (for example):
    >
    >
    > .var$tmin <- ‘minTemp_'
    >
    >
    > And then these names can be accessed from within any of the functions, and
    > this is unlikely to conflict with any R objects the user is defining.
    >
    >
    > Where I am stuck is how/where to define this new environment such that it
    > is made available when the package is loaded. I tried including the
    > following in R/zzz.R:
    >
    >
    > .onLoad <- function(libname, pkgname) {
    >
    >
    >   # define a custom environment for defining the naming of variables
    >
    > .var <- new.env()
    >
    > # default
    >
    > .var$bio <- "bio_"
    >
    > .var$tmin <- "tmin_"
    >
    > .var$tmax <- "tmax_"
    >
    > invisible()
    >
    > }
    >
    >
    > But if I build/install the package, the .var environment is not already
    > created. Any suggestions?
    >
    >
    > Thanks!
    >
    > -Pascal
    >
    >         [[alternative HTML version deleted]]
    >
    > ______________________________________________
    > R-package-devel using r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-package-devel
    
    ______________________________________________
    R-package-devel using r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-package-devel
    


More information about the R-package-devel mailing list