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

Gábor Csárdi c@@rd|@g@bor @end|ng |rom gm@||@com
Tue Jan 29 22:47:08 CET 2019


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



More information about the R-package-devel mailing list