Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R调用Rcpp文件时加载错误,然后在其中回调_R_Rcpp_Argv_Rinside - Fatal编程技术网

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

从R调用Rcpp文件时加载错误,然后在其中回调,r,rcpp,argv,rinside,R,Rcpp,Argv,Rinside,我编写了一个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) 我已尝试通过以下方式启动此

我编写了一个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)
我已尝试通过以下方式启动此脚本:

Rscript demo.r
我收到以下错误:

错误 动态加载(“/tmp/RtmpZl0JKp/sourceCpp-x86\u 64-pc-linux-gnu-0.12.10/sourceCpp\u 90cc33eafd15/sourceCpp\u 2.so”) : 无法加载共享对象“/tmp/RtmpZl0JKp/sourceCpp-x86\u 64-pc-linux-gnu-0.12.10/sourceCpp\u 90cc33eafd15/sourceCpp\u 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
的慢度问题。我有一个R程序,它有一个大的
for
循环,执行速度非常慢。所以,我想把< <代码> > <代码>循环从R改为C++代码。在
for
循环中,我调用了许多R函数。所以,我需要从C++代码调用R程序。因此,顺序是R到C++到R,也就是R到Rcpp到Rin,我错了吗?p> <>为什么?

你不应该在C++中创建一个新的会话,因为你已经有了一个活动的R会话。因此,不包括
Rcpp.h
RInside.h
。在这种情况下,您应该只使用
Rcpp.h

因此,只需使用:

#include <Rcpp.h>

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

最终编辑 事实上,我想解决R的for循环的慢度问题。我有一个R程序,它有一个大的for循环,执行速度非常慢。所以,我想把这个循环从R改成C++代码。在for循环中,我调用了许多R函数。所以,我需要从C++代码调用R程序。因此,顺序是R到C++到R,也就是R到Rcpp到Rin,我错了吗?

只需切换一个调用R函数到C++的循环,并期望加快速度的问题是不正确的。然后,每次遇到R函数时,同一个循环必须伸出,并与R会话通信。本质上,它有效地暂停C++代码,等待R代码执行和结果,然后恢复C++外壳。p> <> P>在这个范例中有效地加速代码的唯一方法是完全写C++中的R函数的“强> > < >”,然后调用C++中的C++等价于< /代码>循环> < /p>


请参阅我之前的备注“R到Rcpp到漂洗液”,这也是一个“否”。永远不要这样做。时期只有“R到Rcpp”或“C++到RInside”是可行的。

如果我能把这个投票提高十几次,我会答应的!
#include <Rcpp.h>

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

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

    return some_r_func("a");
}