Dear R colleagues, I have a set of versioned data objects in a package. Let's call them landmarks.2016 landmarks.2017 I need to have access to both of these objects for different calculations both internal to the package and for end users. The objects are saved as rda files in the package's data folder. I would also like to define an object ? an alias if you will ? that contains the *current* landmarks. This is useful so that some package functions and external scripts do not require editing when the default object changes. For this, somewhere in the package, I would like to do something like: # set the current default landmarks landmarks <- landmarks.2017 I cannot figure out a place to do this in my package because check always complaints that landmarks.2017 does not exist ? I guess lazy loading can only happen after all the R code is evaluated. * Can anyone suggest a workaround that does not involve saving a third redundant rda object into the package's data folder? I have placed a toy example package on GitHub: https://github.com/jefferis/testdataalias With many thanks, Greg Jefferis. -- Gregory Jefferis, PhD Division of Neurobiology MRC Laboratory of Molecular Biology Francis Crick Avenue Cambridge Biomedical Campus Cambridge, CB2 OQH, UK http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis http://jefferislab.org http://www.zoo.cam.ac.uk/departments/connectomics
[R-pkg-devel] referring to a package data object by two names
5 messages · Michael Dewey, Dr Gregory Jefferis, Berry Boessenkool
Dear Greg Do any of the suggestions in section 1.5.3 of Writing R Extensions "Load hooks" work? Michael
On 08/10/2017 08:40, Gregory Jefferis wrote:
Dear R colleagues, I have a set of versioned data objects in a package. Let's call them landmarks.2016 landmarks.2017 I need to have access to both of these objects for different calculations both internal to the package and for end users. The objects are saved as rda files in the package's data folder. I would also like to define an object ? an alias if you will ? that contains the *current* landmarks. This is useful so that some package functions and external scripts do not require editing when the default object changes. For this, somewhere in the package, I would like to do something like: # set the current default landmarks landmarks <- landmarks.2017 I cannot figure out a place to do this in my package because check always complaints that landmarks.2017 does not exist ? I guess lazy loading can only happen after all the R code is evaluated. * Can anyone suggest a workaround that does not involve saving a third redundant rda object into the package's data folder? I have placed a toy example package on GitHub: https://github.com/jefferis/testdataalias With many thanks, Greg Jefferis. -- Gregory Jefferis, PhD Division of Neurobiology MRC Laboratory of Molecular Biology Francis Crick Avenue Cambridge Biomedical Campus Cambridge, CB2 OQH, UK http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis http://jefferislab.org http://www.zoo.cam.ac.uk/departments/connectomics
______________________________________________ R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel --- This email has been checked for viruses by AVG. http://www.avg.com
Dear Michael, Many thanks for writing.
Do any of the suggestions in section 1.5.3 of Writing R Extensions "Load hooks" work?
I thought I had tried all the various permutations in .onLoad but at your prompting I tried some more and this seems to work:
.onLoad <- function (libname, pkgname) {
# set up default landmarks object
assign('landmarks',
envir = parent.env(environment()),
testdataalias::landmarks.2017)
}
It doesn't seem to tickle R CMD check, but I'm not yet convinced that it is 100% safe. Any comments either way are welcome!
Best wishes,
Greg.
Another approach, maybe overkill / not thought through:
latestLandmark <- function()
{
files <- dir(file.path(system.file(package="yourpackage"), "data"),
pattern="landmark", full.names=TRUE)
env <- environment()
dataset <- load(tail(files, 1), envir=env)
return(invisible(get(dataset, envir=env)))
}
landmark <- latestLandmark()
Regards,
Berry
From: R-package-devel <r-package-devel-bounces at r-project.org> on behalf of Gregory Jefferis <jefferis at gmail.com>
Sent: Sunday, October 8, 2017 14:45
To: Michael Dewey
Cc: r-package-devel at r-project.org
Subject: Re: [R-pkg-devel] referring to a package data object by two names
Sent: Sunday, October 8, 2017 14:45
To: Michael Dewey
Cc: r-package-devel at r-project.org
Subject: Re: [R-pkg-devel] referring to a package data object by two names
Dear Michael,
Many thanks for writing.
> Do any of the suggestions in section 1.5.3 of Writing R Extensions "Load hooks" work?
I thought I had tried all the various permutations in .onLoad but at your prompting I tried some more and this seems to work:
.onLoad <- function (libname, pkgname) {
# set up default landmarks object
assign('landmarks',
envir = parent.env(environment()),
testdataalias::landmarks.2017)
}
It doesn't seem to tickle R CMD check, but I'm not yet convinced that it is[[elided Hotmail spam]]
Best wishes,
Greg.
______________________________________________
R-package-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel
Thanks, Berry. Interesting! Both this suggestion and my previous effort seem a little baroque ? fighting against the system.
However after thinking about this some more, I found this:
delayedAssign('landmarks', testdataalias::landmarks.2017)
which feels a bit safer (although does not try to automatically find the most recent object as per your suggestion).
Thanks again,
Greg.
On 9 Oct 2017, at 13:36, Berry Boessenkool <berryboessenkool at hotmail.com> wrote:
Another approach, maybe overkill / not thought through:
latestLandmark <- function()
{
files <- dir(file.path(system.file(package="yourpackage"), "data"),
pattern="landmark", full.names=TRUE)
env <- environment()
dataset <- load(tail(files, 1), envir=env)
return(invisible(get(dataset, envir=env)))
}
landmark <- latestLandmark()
Regards,
Berry
From: R-package-devel <r-package-devel-bounces at r-project.org <mailto:r-package-devel-bounces at r-project.org>> on behalf of Gregory Jefferis <jefferis at gmail.com <mailto:jefferis at gmail.com>>
Sent: Sunday, October 8, 2017 14:45
To: Michael Dewey
Cc: r-package-devel at r-project.org <mailto:r-package-devel at r-project.org>
Subject: Re: [R-pkg-devel] referring to a package data object by two names
Dear Michael,
Many thanks for writing.
Do any of the suggestions in section 1.5.3 of Writing R Extensions "Load hooks" work?
I thought I had tried all the various permutations in .onLoad but at your prompting I tried some more and this seems to work:
.onLoad <- function (libname, pkgname) {
# set up default landmarks object
assign('landmarks',
envir = parent.env(environment()),
testdataalias::landmarks.2017)
}
It doesn't seem to tickle R CMD check, but I'm not yet convinced that it is 100% safe. Any comments either way are welcome!
Best wishes,
Greg.
______________________________________________ R-package-devel at r-project.org <mailto:R-package-devel at r-project.org> mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel <https://stat.ethz.ch/mailman/listinfo/r-package-devel>
-- Gregory Jefferis, PhD Division of Neurobiology MRC Laboratory of Molecular Biology Francis Crick Avenue Cambridge Biomedical Campus Cambridge, CB2 OQH, UK http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis http://jefferislab.org http://www.zoo.cam.ac.uk/departments/connectomics