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

2018-04-16 Thread Jack Wasey
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:

nycflights13::flights
# 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 
variable.

Thanks again for steering me,
Jack



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: 
> 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 mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] segmentation fault when wrapping a big Eigen 2-d array

2018-04-09 Thread Jack Wasey
On Mon, Apr 09, 2018 at 07:09:55AM -0500, Dirk Eddelbuettel wrote:
> 
> On 9 April 2018 at 07:22, Wasey, Jack O wrote:
> | 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.
> 
> From the "well then don't do it" school:  can you cast _before_ you interact
> with R / Rcpp / RcppEigen?   What happens when you try the "big" operation
> entirely in C++ / Eigen and then "merely" transfer a known object (ie an int
> vector/matrix) ?  If that works, try bool and then see if that maps to 
> Logical.
> 
> Some code paths are simply less well trodden.  Int would be first route.
> 
> I may of course be entirely off the page. Early morning  ...
> 
> Dirk
> 
> -- 
> http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

I seem to tread in unusual code paths often, which is
why I appreciate so much your guidance. I can't reproduce the error now
I've converted from integer Eigen matrix to Rcpp::IntegerMatrix, then
within Rcpp, Integer to Logical matrices. 

I naively assumed that if the compiler let me do it, it was a valid
cast. Does this mean I unearthered a bug somewhere down the line of
dependencies?

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] stop and Rf_error cause the same problem

2015-03-13 Thread Jack Wasey
I've seen it said that calling Rf_error is the cause of the new hang 
with Rtools33, quoting my example in (closed) github issue 
https://github.com/RcppCore/Rcpp/issues/276


Just to be clear, the calling Rcpp::stop hung in exactly the same way, 
but I did at that time also demonstrate the bug using Rf_error.


Hope that clarifies things a little,
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


Re: [Rcpp-devel] symbols "major" and "minor" under C++11

2015-03-09 Thread Jack Wasey
Dirk,

I'm on Rcpp 0.11.5

Here are the steps:

1. in R:
```
library(Rcpp)
Rcpp.package.skeleton(attributes = TRUE, example_code = TRUE,
name="majortest")
```

2. create majortest/src/Makevars containing:

```
PKG_CXXFLAGS = $(CXX1XSTD)
```

3. Edit rcpp_hello_world.cpp replacing:
```
List rcpp_hello_world() {
```
with
```
List rcpp_hello_world(CharacterVector major) {
```

4. both `devtools::load_all()` or `R CMD INSTALL majortest` then produce
the previously described compilation errors, with my compiler set to gcc
or clang.

Hope that helps.
Jack

On 03/09/2015 09:52 PM, Dirk Eddelbuettel wrote:
> 
> Jack,
> 
> Which Rcpp release did you use? We just released 0.11.5. Please try that if
> you haven't yet.
> 
> This is "still not reproducible" as I would have to execute a number of steps
> you have not spilled out in an easy-to-follow and unamibguous way. (Your
> description was close though.)
> 
> Dirk
> 

___
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] symbols "major" and "minor" under C++11

2015-03-09 Thread Jack Wasey

Dear All,

I followed your interesting and detailed discussion about the problem in
Rcpp 0.11.3 which resulted in compilation failure with "major", "minor"
or "makedev" as function arguments due to leakage from system headers on
Linux.
https://github.com/RcppCore/Rcpp/issues/227

I am posting follow-up here because I'm not sure if this is a valid bug,
but I am seeing the same problem when compiling with clang 3.7 and gcc
4.9.1 on 64 bit ubuntu 14.10 using C++11.

Simply writing the function which demonstrated #227 isn't enough:
cppFunction("int foo(int major) { return(2*major); }")
does compile with the addition of -std=c++11

However, if I create an Rcpp.package.skeleton with attributes and the
example code, and simply add function argument "CharacterVector major"
to the hello world function:

#include 
using namespace Rcpp;

// [[Rcpp::export]]
List rcpp_hello_world(CharacterVector major) {

CharacterVector x = CharacterVector::create( "foo", "bar" )  ;
NumericVector y   = NumericVector::create( 0.0, 1.0 ) ;
List z= List::create( x, y ) ;

return z ;
}

then the RcppExports.cpp doesn't compile. Here is the clang compiler error:

clang++ -I/usr/share/R/include -DNDEBUG
-I"/usr/local/lib/R/site-library/Rcpp/include"  -std=c++11 -fpic  -g -O2
-fstack-protector-strong -Wformat -Werror=format-security
-D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
RcppExports.cpp:15:44: error: use of undeclared identifier 'major'
__result = Rcpp::wrap(rcpp_hello_world(major));

Here is the code from RcppExports.cpp:
#include 

using namespace Rcpp;

// rcpp_hello_world
List rcpp_hello_world(CharacterVector major);
RcppExport SEXP majmin11_rcpp_hello_world(SEXP majorSEXP) {
BEGIN_RCPP
Rcpp::RObject __result;
Rcpp::RNGScope __rngScope;
Rcpp::traits::input_parameter< CharacterVector >::type major(majorSEXP);
__result = Rcpp::wrap(rcpp_hello_world(major));
return __result;
END_RCPP
}

As it seems to arise from RcppExports, I hope you agree that using
Rcpp.package.skeleton and making a small change constitutes an
appropriate minimal example, since I couldn't reproduce with one single
snippet.

I may well be doing something wrong, but it does seem again like the
system symbols leaking through, since renaming from "major" to "mjr"
fixes the problem (as does removing C++11 compiler flag). I did look at
the merged solution to #227, but honestly it's beyond me to attempt to
dig any deeper.

I realize this is quite a specific and exasperating problem. If you
think it's a bug, I could comment on #227 or open a new issue.

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


Re: [Rcpp-devel] appearance of matrix returned from Rcpp

2015-02-14 Thread Jack Wasey
I figured it out: attribute is called "dimnames" not ".Dimnames".

> .Internal(inspect(rmat))
@34d39b0 10 LGLSXP g0c2 [MARK,NAM(2),ATT] (len=4, tl=0) 1,1,0,0
ATTRIB:
  @32b8228 02 LISTSXP g0c0 [MARK]
TAG: @e83798 01 SYMSXP g1c0 [MARK,LCK,gp=0x4000] "dim" (has value)
@33d78c8 13 INTSXP g0c1 [MARK,NAM(2)] (len=2, tl=0) 2,2
TAG: @e83808 01 SYMSXP g1c0 [MARK,LCK,gp=0x4000] "dimnames" (has value)
@34d3978 19 VECSXP g0c2 [MARK,NAM(2)] (len=2, tl=0)
  @34d3a20 16 STRSXP g0c2 [MARK,NAM(2)] (len=2, tl=0)
@103d408 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "a"
@1217678 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "b"
  @e83b18 00 NILSXP g1c0 [MARK,NAM(2)]
> .Internal(inspect(rcpp_mat))
@34f5f80 10 LGLSXP g0c2 [MARK,NAM(2),ATT] (len=4, tl=0) 1,1,0,0
ATTRIB:
  @32b9100 02 LISTSXP g0c0 [MARK]
TAG: @e83798 01 SYMSXP g1c0 [MARK,LCK,gp=0x4000] "dim" (has value)
@33d76e8 13 INTSXP g0c1 [MARK,NAM(2)] (len=2, tl=0) 2,2
TAG: @1ab0878 01 SYMSXP g0c0 [MARK] ".Dimnames"
@34f5f48 19 VECSXP g0c2 [MARK] (len=2, tl=0)
  @34f5f10 16 STRSXP g0c2 [MARK] (len=2, tl=0)
@103d408 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "a"
@1217678 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "b"
  @e83b18 00 NILSXP g1c0 [MARK,NAM(2)]
___
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] appearance of matrix returned from Rcpp

2015-02-14 Thread Jack Wasey
Hello,

I am trying to return a matrix derived from a std::vector with row
names.

I followed the advice of Kevin and Dirk I read at
https://stackoverflow.com/questions/19864226/convert-stdvector-to-rcpp-matrix

Strangely, the matrix is correct, but when I print it in R, it prints
differently to defining it in R:

#include 
#include 
// [[Rcpp::export]]
SEXP make_mat(std::vector x, std::vector n, int nrow,
int ncol) {
Rcpp::LogicalVector y = Rcpp::wrap(x);
y.attr("dim") = Rcpp::Dimension(nrow, ncol);
y.attr(".Dimnames") = Rcpp::List::create(n, R_NilValue);
return y;
}

rcpp_mat <- make_mat(c(T,T,F,F), c("a", "b"), 2, 2)
rcpp_mat
 [,1]  [,2]
[1,] TRUE FALSE
[2,] TRUE FALSE
attr(,".Dimnames")
attr(,".Dimnames")[[1]]
[1] "a" "b"

attr(,".Dimnames")[[2]]
NULL

rmat
  [,1]  [,2]
a TRUE FALSE
b TRUE FALSE

> typeof(rmat)
[1] "logical"
> typeof(rcpp_mat)
[1] "logical"
> class(rmat)
[1] "matrix"
> class(rcpp_mat)
[1] "matrix"

identical(rmat, rcpp_mat)
[1] FALSE

What's going on here, please?

Ideally, I'd rather construct a LogicalMatrix directly from
std::vector, but I don't think you have that constructor.

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