0

I'm working on a package (let's call it myPackage) that needs to refer to external data, which is too big to incorporate into the package itself (it's a lot of netCDF files).

Because of this I have an internal PATH variable (which I initiate in /data-raw/, which saves it to sysdata.rda, and I've turned off lazy loading). When asked to get data, any function can then use this PATH to find the data.

I want the user to be able to specify their path, so I wrote a function:

setpath<-function(path){
  myPackage:::PATH = path
}

Doing setpath("C:/") doesn't work. I get the error: Error in setpath("C:/") : object 'myPackage' not found.

I've also tried the following alternative function:

setpath<-function(path){
  PATH = path
}

This way, the variable myPackage:::PATH never changes.

How should I be doing this? Is internal data read-only?

Joezerz
  • 129
  • 6
  • It is possible to modify objects in another namespace (in this case `myPackage`) using `utils::assignInNamespace()`, but the documentation strongly discourages using it in production code. What you're asking is how to set a global variable, which is answered in [Global variables in packages in R](https://stackoverflow.com/q/12598242) and [Global variables in R](https://stackoverflow.com/q/1236620). But the short answer is that it's a [bad idea](https://stackoverflow.com/q/5526322). Instead add a `path` argument to all functions that need these files to keep them self-contained. – Joe Roe Feb 03 '21 at 10:59

1 Answers1

1

You can use options(). Create an option with

options(myPackageRepositoryPath = "some/path")

and retrieve it

path <- getOption(myPackageRepositoryPath)

The same way as you set an option, you also can overwrite an option:

setpath<-function(path){
  options(myPackageRepositoryPath = path)
}
khoffmann
  • 173
  • 5