Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Altering internal data in R package

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...