Where to create package environment variables?

痞子三分冷 提交于 2019-12-21 05:23:15

问题


I'm doing a data analysis and created a package to store my vignettes and data, as explained here.

I want to set some variables that would be available to all my package functions.

These variables define: the path to data sets, the measurements characteristics (such as probes positions), physical constants and so on.

I have read that one recommended way to store such variables is to use environments.

The question is, where do I put the script that creates the environment?

I thought about putting it in the onLoad method, to be sure it's executed when the package is loaded.


回答1:


If you put it in the .onLoad function (not method), you'll have to use the assign function to ensure the environment gets created in your package namespace.

.onLoad <- function(libname, pkgname)
{
    # ...
    assign("myPackageEnvironment", new.env(), parent.env())
    # ...
}

But you can also just put it in open code:

myPackageEnvironment <- new.env()

Informally, you can think of your package's .R files as being sourced one after another into the environment of your package namespace. So any statements that run in open code will create objects there directly.



来源:https://stackoverflow.com/questions/41954302/where-to-create-package-environment-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!