[R-pkg-devel] using package data in package code
Linus Chen
||nu@@|@chen @end|ng |rom gm@||@com
Fri Jun 7 00:29:24 CEST 2019
Dear all,
I am trying to understand the usage of package data.
Suppose I create an object and save it
x <- 3
save(x, 'sysdata.rda')
and then put it into mypac/data/ directoy.
And suppose in my packge code I have
f <- function(x) x+1
Then, when the package has been built, installed, and loaded,
the user get access to mypac::f and mypac::x, and
y <- mypac::f(mypac::x)
will give 4.
However, if I put the same code into the package, with the purpose to
create an object mypac::y, the package would not build, with the error
message: object 'x' not found.
So my question is: Is there a mechanism for the package data to be accessed by
the package code?
I have tried to look for answer in "Writing R Extensions",
and especially, I have read section 1.1.6 "Data in packages" carefully,
but no answer for my question...
In my real package, certainly x and f is non-trivial. Basically x is
kind of a look-up table.
The 'y' that I wish to provide with the package to the user is a S4 object.
And 'f', the function am using to create 'y', is itself a non-trivial
function defined in the package.
The work-around I can figure out are the following two:
1. put 'x' in a separate package, which 'mypac' will import.
Then the package code "y <- f(x)".
But it feels bad to have two packages for one integral set of information.
2. Do the whole thing in two pass, in the first round the package
contain only x anf f.
Then, load the package, run y<-f(x), and add it to 'sysdata.rda'.
But this way, the two-pass operation has to be repeated each time
the package is modified,
even if the change does not affect 'x', and only affect 'f'.
So is there a more elegant way to deal with this?
I would be grateful to any hints, or useful links. Thank you!
Best regards,
Lei Chen
More information about the R-package-devel
mailing list