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().

Kevin

On Fri, Apr 13, 2018 at 10:10 PM, Wasey, Jack O <j...@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@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to