[R-pkg-devel] define an environment in .onLoad
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 at 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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel