简体   繁体   English

从R加载错误调用Rcpp文件,然后在其中回调

[英]Load error calling Rcpp file from R then call back in it

I wrote a R program that calls hello1() , which is an Rcpp function contained in demo2.cpp program 我写了一个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)

my demo2.cpp is : 我的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);
}

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 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:未定义符号:_ZNVRInside源-> withVisible-> eval-> eval-> dyn.load执行暂停

In fact, I want to solve slowness of R 's for loop. 其实,我想解决的[R的缓慢for循环。 I have an R program that has a large for loop and it execute very slowly. 我有一个带有大型for循环的R程序,它执行得非常慢。 So, I want to change that for loop from R to C++ code. 因此,我想将for循环从R更改for C ++代码。 Within the for loop, I call many R functions. for循环中,我调用了许多R函数。 So, I need to call from C++ code to R program. 因此,我需要从C ++代码调用R程序。 Thus, the order is R to C++ to R , that is R to Rcpp to Rinside , am I wrong? 因此,顺序为RC ++R,R键 RCPPRinside,我错了?

Why? 为什么?

You should not create a new session of R from within C++ given that you already have an active R session. 应该从C创建R的新会话++给你已经有一个活跃的R对话。 With this being said, do NOT include both Rcpp.h and RInside.h . 有了这个虽这么说, 包括既Rcpp.hRInside.h In this case, you should only use Rcpp.h . 在这种情况下,您应该只使用Rcpp.h

So, simply use: 因此,只需使用:

#include <Rcpp.h>

// [[Rcpp::export]]
int  hello1(int argc, string path)
{
    const char *argv[] = {path.c_str()};
    return(111);
}

Edit 1 编辑1

Per a later comment that was deleted, I think you want to use some R function within C++ . 对于后来删除的注释,我认为您想在C ++中使用一些R函数。 To do that use Rcpp 's Function class. 为此,请使用RcppFunction类。 Make sure to load the R function into memory first by running the declaration. 确保首先通过运行声明将R函数加载到内存中。 After doing this, then compile the following C++ code: 完成此操作后,然后编译以下C ++代码:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::CharacterVector hello1()
{
    Rcpp::Function some_r_func("ssss");

    return some_r_func("a");
}

Final Edit 最终编辑

In fact, I want to solve slowness of R's for loop. 实际上,我想解决R fors循环的慢性。 I have an R program that has a large for loop and it execute very slowly. 我有一个带有大型for循环的R程序,它执行得非常慢。 So, I want to change that for loop from R to C++ code. 因此,我想将for循环从R更改为C ++代码。 Within the for loop, I call many R functions. 在for循环中,我调用了许多R函数。 So, I need to call from C++ code to R program. 因此,我需要从C ++代码调用R程序。 Thus, the order is R to C++ to R, that is R to Rcpp to Rinside, am I wrong? 因此,从R到C ++到R的顺序是从R到Rcpp到Rinside的顺序,对吗?

The issue with just switching a loop that calls R functions from R to C++ and expecting a speed up is incorrect. 仅将调用R函数的循环从R切换到C ++并期望加速的问题是不正确的。 The same loop must then reach out each time it encounters an R function and communicate with the R session. 然后,每次遇到R函数并与R会话进行通信时,同一循环必须伸出。 In essence, it effectively pauses the C++ code, waits for the R code to execute and result, and then resume C++ shell. 本质上,它有效地暂停了C ++代码,等待R代码执行并生成结果,然后恢复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. 在这种范例中有效加速代码的唯一方法是完全用C ++编写所有 R函数,然后在C ++ for循环中调用它们的C ++等效项。

See my previous remarks for "R to Rcpp to Rinside", which is again a NO. 请参阅我之前对“ R到Rcpp到Rinside”的评论,这也是一个NO。 Never do this. 永远不要这样做。 Period. 期。 Only "R to Rcpp" or "C++ to RInside" are viable. 仅“ R to Rcpp”或“ C ++ to RInside”是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM