Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to lazyload variables from inst/extdata in R package

I have a file helper.RData file in my inst/extdata that contains variables and datasets to be used by the functions in my package, but not meant to be accessed by the user.

I load it at the beginning of the package using:

load(system.file("extdata","helper.RData", package = "mypackage"))

As the file is big this takes quite a bit of time and it is especially annoying during development (I use quite a loot the function load_all() from the devtools package).

I would rather prefer to have it lazy loaded so that the file is loaded only when actually needed.

How can I do that?

like image 418
lucacerone Avatar asked Feb 05 '14 16:02

lucacerone


People also ask

How do I read data from a package in R?

Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package.

What does Extdata mean in R?

extdata: Extra data used to calculate ID numbers in Yassai et al.'s nomenclature.

What does Extdata mean?

"extdata" is presumably short for "external data". However, this doesn't mean that you need to use "extdata" when you are structuring your own code; you only need it when finding the files that are included by the package.

What is lazy data in R?

lazyData: A LazyData Facility A single function is is included, requireData, which is a drop-in replacement for base::require, but carrying the additional functionality. By default, it suppresses package startup messages as well.


1 Answers

Before being able to lazy-load your data you have to save your variables in a database that supports lazy load.

You can do this using the function tools:::makeLazyLoadDB and later the function lazyLoad.

To create the lazy load database. Say you have the variables X and Y, the you have to create an environment that contains them:

e=new.env(parent=emptyenv())
e$X = X
e$Y = Y

next you create the database:

tools:::makeLazyLoadDB(e,"DBNAME")

of course you can change DBNAME.

You can the import it in R using lazyLoad("DBNAME").

like image 76
lucacerone Avatar answered Sep 28 '22 11:09

lucacerone