【发布时间】:2017-04-05 01:51:38
【问题描述】:
我写了一个R程序调用hello1(),这是demo2.cpp程序中包含的一个Rcpp函数
library(Rcpp)
ssss <- function(dd)
{
return(paste("hello ",dd))
}
sourceCpp(file='demo2.cpp')
currentpath <- "/home/xuyan/R/parallel/"
aaa <-hello1(0,currentpath)
print(aaa)
我的demo2.cpp 是:
#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);
}
我尝试使用以下命令启动此脚本:
Rscript demo.r
我收到以下错误:
错误 dyn.load("/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': /tmp/RtmpZl0JKp/sourceCpp-x86_64-pc-linux-gnu-0.12.10/sourcecpp_90cc33eafd15/sourceCpp_2.so: 未定义符号:_ZN7RInsideD1Ev 调用:sourceCpp -> source -> withVisible -> eval -> eval -> dyn.load 执行停止
其实我是想解决R的for循环慢的问题。我有一个 R 程序,它有一个大的 for 循环,它执行非常缓慢。因此,我想将 for 循环从 R 更改为 C++ 代码。在for 循环中,我调用了许多R 函数。所以,我需要从 C++ 代码调用 R 程序。因此,顺序是R to C++ to R,即R to Rcpp 到 Rinside,我错了吗?
为什么?
【问题讨论】: