[Rcpp-devel] Trouble passing arguments to R functions that have been passed in (maybe)
Anthony Lee
awllee at gmail.com
Mon Sep 10 18:02:35 CEST 2012
Hello,
I'm quite new to Rcpp (and R in fact) and I'm having a little bit of
trouble understanding what is going on in some of my code. I have
written a fairly small example that exhibits the troublesome behaviour
using inline (below). The code doesn't do anything interesting, but
runCode() does cause a "memory not mapped" error. Other times I get an
"unimplemented type 'NULL' in 'coerceToInteger'" error.
It seems to be the case that sometimes the values N and true do not make
it back safely to R when calling bar within foo and get transformed into
NULL or doubles, etc. I still have issues if I "PROTECT" their
conversion to SEXP via wrap.
I know it is not particularly good to have C++ functions calling R
functions that have been passed in, but this is a very useful feature
for me and I have been very happy with Rcpp until this starting happening.
Any help on what mistake I've made would be greatly appreciated.
Thanks,
Anthony
Code: (note that removing X and Y seems to make the code more stable)
require(inline)
foo <- cxxfunction(
signature(N_in = "numeric", n_in = "numeric", bar_in = "function"),
body='
BEGIN_RCPP
int N = as<int>(N_in);
int n = as<int>(n_in);
Function bar(bar_in);
IntegerMatrix A(N,n);
GenericMatrix X(N,n);
GenericMatrix Y(N,n);
A(_,0) = seq_len(N-1);
for (int i = 1; i < n; i++) {
A(_,i) = as<IntegerVector>(bar(A(_,i-1),N,true));
}
return wrap(A);
END_RCPP
', plugin = "Rcpp" )
translate <- function(size,cond,vs) {
if (cond) {
return((vs + 1) %% size)
} else {
return((vs - 1) %% size)
}
}
bar <- function(vs,M,cond) {
return(translate(size=M,cond=cond,vs=vs))
}
runCode <- function() {
for (i in 1:5) {
for (j in 1:10) {
print(paste(i,j,sep=" "));
B <- foo(i*100,j*100,bar);
}
}
}
# runCode()
More information about the Rcpp-devel
mailing list