Newbie to C/C++ and Rcpp.
I'm currently trying to modify examples I find (in this case I modified the "yada" module example http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-modules.pdf) and extend them to test my understanding.
The example I have currently compiles but, does not have the expected behaviour. My guess it that I'm missing something, but I can't determine what is missing from I find in the docs. Any help would be much appreciated.
the example code is below.
library(inline)
fx=cxxfunction(,plugin="Rcpp",include='#include<Rcpp.h>
#include<string>
typedef struct containerForChars {const char *b;} containChar;
containChar cC;
const char* toConstChar(std::string s){return s.c_str();}
void setB(std::string s){
cC.b = toConstChar(s);
}
std::string getB(void){
std::string cs = cC.b;
return cs;
}
RCPP_MODULE(ex1){
using namespace Rcpp;
function("setB",&getB);
function("getB",&getB);
}')
mod=Module("ex1",getDynLib(fx))
f<-mod$setB
g<-mod$getB
f("asdf")
g()
Instead of f("asdf") setting cC.b to "asdf", I get the following error,
Error in f("asdf") : unused argument ("asdf")
My hope is that the arg to f() will be set as the value for cC.b, and g() will retrieve or get the value I set with f. My guess is that whatever magic that Module and RCPP_MODULE do isn't capable of using the struct I defined. I guess hoping for it to work wasn't enough :P.
Common typo. Instead of
function("setB",&getB);
function("getB",&getB);
do
function("setB",&setB); # set, not get
function("getB",&getB);
and then everything works:
R> f("asdf")
R> g()
[1] "asdf"
R>
I'd also add library(inline) at the top.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With