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

Kevin Ushey kevinushey at gmail.com
Mon Apr 16 04:18:31 CEST 2018

The environments called 'package:foo' are associated with attached
packages; that is, they're located on the search path. You likely want to
find the actual package namespace.

I think we have a static function Environment::namespace_env() for this. Or
you can just call back to R with requireNamespace().


On Fri, Apr 13, 2018 at 10:10 PM, Wasey, Jack O <jack at jackwasey.com> wrote:

> 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
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20180415/3855ad73/attachment.html>

More information about the Rcpp-devel mailing list