Reputation: 21
I wrote a R program that calls hello1()
, which is an Rcpp function contained in demo2.cpp
program
library(Rcpp)
ssss <- function(dd)
{
return(paste("hello ",dd))
}
sourceCpp(file='demo2.cpp')
currentpath <- "/home/xuyan/R/parallel/"
aaa <-hello1(0,currentpath)
print(aaa)
my demo2.cpp
is :
#include <Rcpp.h>
#include <string>
#include <RInside.h>
using namespace std;
using namespace Rcpp;
// [[Rcpp::export]]
int hello1(int argc,string path)
{
const char *argv[] = {path.c_str()};
RInside R(argc,argv);
R["txt"] = "Hello, world!\n";
R["sourcer"] = "demo.r";
R.parseEvalQ("source(sourcer)");
string str = Rcpp::as<string>(R.parseEval("ssss(txt)"));
cout << "result is" << str << endl;
return(111);
}
I've tried to launch this script with:
Rscript demo.r
I receive the following error:
Error in dyn.load("/tmp/RtmpZl0JKp/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_90cc33eafd15/sourceCpp_2.so") : unable to load shared object '/tmp/RtmpZl0JKp/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_90cc33eafd15/sourceCpp_2.so': /tmp/RtmpZl0JKp/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_90cc33eafd15/sourceCpp_2.so: undefined symbol: _ZN7RInsideD1Ev Calls: sourceCpp -> source -> withVisible -> eval -> eval -> dyn.load Execution halted
In fact, I want to solve slowness of R's for
loop. I have an R program that has a large for
loop and it execute very slowly. So, I want to change that for
loop from R to C++ code. Within the for
loop, I call many R functions. So, I need to call from C++ code to R program. Thus, the order is R to C++ to R, that is R to Rcpp to Rinside, am I wrong?
Why?
Upvotes: 2
Views: 407
Reputation: 20746
You should not create a new session of R from within C++ given that you already have an active R session. With this being said, do NOT include both Rcpp.h
and RInside.h
. In this case, you should only use Rcpp.h
.
So, simply use:
#include <Rcpp.h>
// [[Rcpp::export]]
int hello1(int argc, string path)
{
const char *argv[] = {path.c_str()};
return(111);
}
Per a later comment that was deleted, I think you want to use some R function within C++. To do that use Rcpp's Function
class. Make sure to load the R function into memory first by running the declaration. After doing this, then compile the following C++ code:
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::CharacterVector hello1()
{
Rcpp::Function some_r_func("ssss");
return some_r_func("a");
}
In fact, I want to solve slowness of R's for loop. I have an R program that has a large for loop and it execute very slowly. So, I want to change that for loop from R to C++ code. Within the for loop, I call many R functions. So, I need to call from C++ code to R program. Thus, the order is R to C++ to R, that is R to Rcpp to Rinside, am I wrong?
The issue with just switching a loop that calls R functions from R to C++ and expecting a speed up is incorrect. The same loop must then reach out each time it encounters an R function and communicate with the R session. In essence, it effectively pauses the C++ code, waits for the R code to execute and result, and then resume C++ shell.
The only way to effectively speed up code in this paradigm is to completely write all of the R functions in C++ and then call their C++ equivalents within the C++ for
loop.
See my previous remarks for "R to Rcpp to Rinside", which is again a NO. Never do this. Period. Only "R to Rcpp" or "C++ to RInside" are viable.
Upvotes: 2