在哪里创建包环境变量? [英] Where to create package environment variables?

查看:113
本文介绍了在哪里创建包环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行数据分析,并创建了一个包来存储我的小插图和数据,如

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?

我考虑过将其放在onLoad方法中,以确保在加载程序包时执行它.

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

推荐答案

如果将其放在.onLoad函数(不是方法)中,则必须使用assign函数来确保在以下环境中创建环境您的程序包名称空间.

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()

非正式地,您可以认为包的.R文件是一个接一个地source进入包名称空间环境的文件.因此,任何以开放代码运行的语句都将直接在其中创建对象.

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.

这篇关于在哪里创建包环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆