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

2018-04-13 Thread Wasey, Jack O
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()

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] segmentation fault when wrapping a big Eigen 2-d array

2018-04-09 Thread Wasey, Jack O
Dear Rcpp developers,

I'm having trouble with a reproducible error when wrapping a big Eigen dense 
matrix.

Rcpp::LogicalMatrix mat_out_bool = Rcpp::wrap(result);

Thread 1 "R" received signal SIGSEGV, Segmentation fault.
coerceToLogical (v=0x7fffa1a1c010) at coerce.c:441

Where 'result' is an Eigen::MatrixXi of size 472132x30, which is the result of 
a sparse x dense matrix multiplication.
DenseMap result = visit_codes_sparse * map;

The backtrace begins:
#0  coerceToLogical (v=0x7fffa1a1c010) at coerce.c:441
#1  Rf_coerceVector (v=, type=) at coerce.c:1243
#2  0x7fffdc660515 in Rcpp::internal::basic_cast<10> (x=0x7fffa1a1c010) at 
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/r_cast.h:66
#3  0x7fffdc65edc5 in Rcpp::internal::r_true_cast<10> (x=0x7fffa1a1c010) at 
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/r_cast.h:95
#4  0x7fffdc664a9d in Rcpp::r_cast<10> (x=0x7fffa1a1c010) at 
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/r_cast.h:163
#5  0x7fffdc666f99 in Rcpp::Matrix<10, Rcpp::PreserveStorage>::Matrix 
(this=0x7fff23a0, x=0x7fffa1a1c010)
at /usr/local/lib/R/site-library/Rcpp/include/Rcpp/vector/Matrix.h:53
#6  0x7fffdc68603e in icd9Comorbid_alt_MatMul (icd9df=..., icd9Mapping=..., 
visitId="visit_id", icd9Field="code", threads=8, chunk_size=256, 
omp_chunk_size=1)
at comorbid_alt_MatMul.cpp:271
#7  0x7fffdc64a597 in _icd_icd9Comorbid_alt_MatMul_try 
(icd9dfSEXP=0x58590cd8, icd9MappingSEXP=0x5839d9d0, 
visitIdSEXP=0x5a25e2b8, icd9FieldSEXP=0x5c066298,
threadsSEXP=0x623279f8, chunk_sizeSEXP=0x586122e8, 
omp_chunk_sizeSEXP=0x58612258) at RcppExports.cpp:347

I would love to give a small reproducible example, but at this point, I can 
only trigger the problem with a big data set (which makes valgrind unreasonably 
slow), and only by repeating the computation, as sometimes it does succeed. The 
code is in the eigen-sparse branch of https://github.com/jackwasey/icd . I do 
not expect anyone to download and debug my code (which FWIW compiles without 
any warnings), but I would like to know whether, in principle, I am doing 
something wrong with the integer to logical 'cast' when wrapping, or if this is 
possibly an Rcpp or RcppEigen bug. I can induce the crash on a server with huge 
amounts of RAM, and on Mac and Linux platforms, with and without OpenMP enabled.

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