Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to create package environment variables?

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.

like image 389
Ben Avatar asked Jan 31 '17 09:01

Ben


People also ask

Where can I find environment variables?

In the System > About window, click the Advanced system settings link at the bottom of the Device specifications section. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab.

Which package will be used to get environment variables?

Environment variables are implemented through the os package, specifically os. environ.

What is environment variable in application packaging?

Environment variables are settings or paths that are set outside of a program and can be globally accessed by any software. A quick way to know the environment variables present on a machine is to open the CMD and type set.

How do I set environment variable in CMD?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


1 Answers

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.

like image 200
Hong Ooi Avatar answered Nov 04 '22 19:11

Hong Ooi