Thanks for the reply. The solution of requiring the namespace from within Rcpp 
hadn't occurred to me.

I had used Environment::namespace_env("pkg_name") before, but I think the 
problem here is that lazy-loaded (and maybe other elements we expect in R) are 
not available if this is done from Rcpp. E.g.

# (using R --vanilla)
Rcpp::cppFunction('Rcpp::Environment getUnloaded2() { return 
Rcpp::Environment::namespace_env("nycflights13"); }')
ls(envir = getUnloaded2())
# empty, whereas the following:

# pulls the data, with the package not being loaded or attached.

To close this thread, I think what the latter command does differently to Rcpp 
is to load the package, because sessionInfo() after running the second command, 
includes nycflights13 in the section "loaded via a namespace (and not 
attached):" This is not the case after pulling the environment into an Rcpp 

Thanks again for steering me,

On 04/15/2018 10:18 PM, Kevin Ushey wrote:
> 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 < 
> <>> 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: 
> <> 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 mailing list

Reply via email to