<div dir="ltr">Sorry, you're correct -- lazy-loaded data gets extracted not from the package namespace but from the 'lazydata' portion of the namespace info.<div><br></div><div>Ultimately, I think you'll just want to call back to R to get what you want, e.g.</div><div><br></div><div> getExportedValue("nycflights13", "flights")</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 16, 2018 at 12:48 PM, Jack Wasey <span dir="ltr"><<a href="mailto:jack@jackwasey.com" target="_blank">jack@jackwasey.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for the reply. The solution of requiring the namespace from within Rcpp hadn't occurred to me.<br>
<br>
I had used Environment::namespace_env("<wbr>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.<br>
<br>
# (using R --vanilla)<br>
Rcpp::cppFunction('Rcpp::<wbr>Environment getUnloaded2() { return Rcpp::Environment::namespace_<wbr>env("nycflights13"); }')<br>
ls(envir = getUnloaded2())<br>
# empty, whereas the following:<br>
<br>
nycflights13::flights<br>
# pulls the data, with the package not being loaded or attached.<br>
<br>
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 variable.<br>
<br>
Thanks again for steering me,<br>
Jack<br>
<span class=""><br>
<br>
<br>
On 04/15/2018 10:18 PM, Kevin Ushey wrote:<br>
> 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.<br>
> <br>
> I think we have a static function Environment::namespace_env() for this. Or you can just call back to R with requireNamespace().<br>
> <br>
> Kevin<br>
> <br>
</span><span class="">> On Fri, Apr 13, 2018 at 10:10 PM, Wasey, Jack O <<a href="mailto:jack@jackwasey.com">jack@jackwasey.com</a> <mailto:<a href="mailto:jack@jackwasey.com">jack@jackwasey.com</a>>> wrote:<br>
> <br>
> Dear all,<br>
> <br>
</span>> 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: <a href="http://dirk.eddelbuettel.com/code/rcpp/html/Environment_8h_source.html" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com/<wbr>code/rcpp/html/Environment_8h_<wbr>source.html</a> <<a href="http://dirk.eddelbuettel.com/code/rcpp/html/Environment_8h_source.html" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com/<wbr>code/rcpp/html/Environment_8h_<wbr>source.html</a>> 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.<br>
<span class="">> <br>
> I use the package nycflights13 as an example. Running `sessionInfo()` shows the package nycflights13 is not loaded, then I run:<br>
> R --vanilla<br>
> > a <- nycflights13::flights<br>
> <br>
> Which completes without error. In Rcpp, I cannot even get a handle on the package namespace if it is not loaded.<br>
> <br>
> Rcpp::cppFunction('Rcpp::<wbr>Environment getUnloaded() { Rcpp::Environment env("package:nycflights13"); return env;}')<br>
> getUnloaded()<br>
> Error in getUnloaded() :<br>
> Cannot convert object to an environment: [type=character; target=ENVSXP].<br>
> <br>
> library(nycflights13)<br>
> Rcpp::cppFunction('Rcpp::<wbr>Environment getUnloaded() { Rcpp::Environment env("package:nycflights13"); return env;}')<br>
> getUnloaded()<br>
> <environment: package:nycflights13><br>
> attr(,"name")<br>
> [1] "package:nycflights13"<br>
> attr(,"path")<br>
> [1] "/Library/Frameworks/R.<wbr>framework/Versions/3.4/<wbr>Resources/library/<wbr>nycflights13"<br>
> ><br>
> <br>
> 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.<br>
> <br>
> 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.<br>
> <br>
> Using Rcpp 0.12.16, R 3.4.3 on Mac.<br>
> <br>
> Any advice appreciated. Thanks,<br>
> <br>
> Jack<br>
> <br>
> <br>
> ______________________________<wbr>_________________<br>
> Rcpp-devel mailing list<br>
</span>> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-<wbr>project.org</a> <mailto:<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-<wbr>forge.r-project.org</a>><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-<wbr>project.org/cgi-bin/mailman/<wbr>listinfo/rcpp-devel</a> <<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-<wbr>project.org/cgi-bin/mailman/<wbr>listinfo/rcpp-devel</a>><br>
> <br>
> <br>
</blockquote></div><br></div>