[Rcpp-devel] using an unloaded package's lazy-load data in Rcpp function

Wasey, Jack O jack at jackwasey.com
Sat Apr 14 07:10:21 CEST 2018


Dear all,

It seems I can reference lazy-loaded data from an unloaded, unattached package from R, but not using the equivalent Rcpp as described in the quickref document. I checked out: http://dirk.eddelbuettel.com/code/rcpp/html/Environment_8h_source.html and even the scant references to environments in the C API section of "Writing R Extensions" and doesn't see what I'm missing. I suspect this is a limitation of R's C interface, not necessarily Rcpp itself.

I use the package nycflights13 as an example. Running `sessionInfo()` shows the package nycflights13 is not loaded, then I run:
R --vanilla
> a <- nycflights13::flights

Which completes without error. In Rcpp, I cannot even get a handle on the package namespace if it is not loaded.

Rcpp::cppFunction('Rcpp::Environment getUnloaded() { Rcpp::Environment env("package:nycflights13"); return env;}')
getUnloaded()
Error in getUnloaded() : 
  Cannot convert object to an environment: [type=character; target=ENVSXP].

library(nycflights13)
Rcpp::cppFunction('Rcpp::Environment getUnloaded() { Rcpp::Environment env("package:nycflights13"); return env;}')
getUnloaded()
<environment: package:nycflights13>
attr(,"name")
[1] "package:nycflights13"
attr(,"path")
[1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/nycflights13"
>

I also found that simple using `loadNamespace` was insufficient for the Rcpp approach to work, but after `library` or `attachNamespace` the above function does work.

Am I treading in another rare code path? All I want to do is use a lazy-loaded data set in an Rcpp function when the package may not be loaded when called. Should I just be passing it as a parameter to the function? This seems ugly.

Using Rcpp 0.12.16, R 3.4.3 on Mac.

Any advice appreciated. Thanks,

Jack




More information about the Rcpp-devel mailing list