我编写了一个函数Rcpp并用它编译inline.现在,我想在不同的内核上并行运行它,但是我遇到了一个奇怪的错误.这里有一个小例子,其中功能funCPP1可以编译和运行本身很好,但不能被调用snow的clusterCall函数.该函数作为单个进程运行良好,但在并行运行时会出现以下错误:
Error in checkForRemoteErrors(lapply(cl, recvResult)) :
2 nodes produced errors; first error: NULL value passed as symbol address
Run Code Online (Sandbox Code Playgroud)
以下是一些代码:
## Load and compile
library(inline)
library(Rcpp)
library(snow)
src1 <- '
Rcpp::NumericMatrix xbem(xbe);
int nrows = xbem.nrow();
Rcpp::NumericVector gv(g);
for (int i = 1; i < nrows; i++) {
xbem(i,_) = xbem(i-1,_) * gv[0] + xbem(i,_);
}
return xbem;
'
funCPP1 <- cxxfunction(signature(xbe = "numeric", g="numeric"),body = src1, plugin="Rcpp")
## Single process …Run Code Online (Sandbox Code Playgroud) 我在R中有一个列表x <-list(c(1,2,3),c(4,5),c(5,5),c(6)).我想将列表输入到Rcpp并将它们作为平均向量返回,c(2,4,5,5,6).
我不知道如何处理Rcpp中的列表.我收到了错误消息,有人可以查看我的代码吗?
library(inline)
fx = cxxfunction(signature(x='List'), body =
'
Rcpp::List xlist(x);
int n = xlist.size();
double res[n];
for(int i=0; i<n; i++) {
Rcpp NumericVector y(xlist[i]);
int m=y.size();
res[i]=0;
for(int j=0; j<m; j++){
res[i]=res[i]+y[j]
}
}
return(wrap(res));
'
, plugin='Rcpp')
x<-list(c(1,2,3), c(4,5), c(5,5), c(6))
fx(x)
Run Code Online (Sandbox Code Playgroud) 我怎样才能在Rcpp中调整大小NumericVector?
当我使用此push_back功能时,程序会变慢.但没有.resize()或.reserve()功能.(当我已经有一个NumericVector所需的大小时,我可以使用copy-constructor来获得正确的大小NumericVector.这种情况比使用时快得多push_back)
我试图将RcppArmadillo矢量(例如arma::colvec)转换为Rcpp矢量(NumericVector).我知道我可以先转换arma::colvec为SEXP然后转换SEXP为NumericVector(例如as<NumericVector>(wrap(temp)),假设temp是一个arma::colvec对象).但是这样做的好方法是什么?
我想这样做只是因为我不确定是否可以将arma::colvec对象作为参数传递给Rcpp::Function对象.
我从另一个问题中拿了这个例子.我正在用Rcpp构建一个R包.我有一个像fun1(下面)这样的函数,我想把它放到自己的.cpp文件中.然后我想调用fun1其他函数(如下fun()所示).我想fun1在一个单独的文件中,因为我将从不同.cpp文件中的几个Rcpp函数调用它.是否有某些include语句和我需要做的事情才能使fun1函数在.cppwhere fun()位置可访问?谢谢.
library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"),
plugin="Rcpp",
body="
int fun1( int a1)
{int b1 = a1;
b1 = b1*b1;
return(b1);
}
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
")
Run Code Online (Sandbox Code Playgroud)
所以对于我的代码,我将有两个.cpp文件:
#include <Rcpp.h>
using namespace Rcpp;
// I think I need something here to make fun1.cpp available?
// [[Rcpp::export]]
Rcpp::NumericVector fun(Rcpp::NumericVector …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何将RcppArmadillo colvec作为标准R矢量返回.我希望我可以进行类型转换,as<NumericVector>(wrap())但我仍然最终得到了R矩阵的对象.这里有一些代码来展示我尝试过的内容(部分受到前一个问题的启发):
// [[Rcpp::export]]
List testthis(NumericVector x) {
arma::colvec y = x;
arma::vec z = x;
return List::create(Rcpp::Named("y1")=y,
Rcpp::Named("y2")=wrap(y),
Rcpp::Named("y3")=as<NumericVector>(wrap(y)),
Rcpp::Named("z1")=z,
Rcpp::Named("z2")=arma::trans(z),
Rcpp::Named("z3")=as<NumericVector>(wrap(z))
);
}
Run Code Online (Sandbox Code Playgroud)
如果我查看输出,我会得到以下所有R矩阵对象.我可以把它投射到R矢量吗?
> testthis(c(1:3))
$y1
[,1]
[1,] 1
[2,] 2
[3,] 3
$y2
[,1]
[1,] 1
[2,] 2
[3,] 3
$y3
[,1]
[1,] 1
[2,] 2
[3,] 3
$z1
[,1]
[1,] 1
[2,] 2
[3,] 3
$z2
[,1] [,2] [,3]
[1,] 1 2 3
$z3
[,1]
[1,] 1
[2,] 2
[3,] 3
Run Code Online (Sandbox Code Playgroud) > a<-matrix(c(1:9),3,3)
> a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> a[3,]*a[,3] # I expect 1x1 matrix as result of this.
[1] 21 48 81
> class(a)
[1] "matrix"
> class(a[3,])
[1] "integer"
Run Code Online (Sandbox Code Playgroud)
在R中,1维矩阵变为向量.我可以避免这个吗?我想保留1-D矩阵作为矩阵.实际上,我需要向RcppArmadillo抛出许多种矩阵,甚至是零D矩阵.将矩阵单独更改为向量是我的问题.
我希望这不是太明显,因为我整天搜索并找不到答案.
说我有以下R文件:
library(Rcpp)
sourceCpp("cfile.cpp")
giveOutput(c(1,2,3))
Run Code Online (Sandbox Code Playgroud)
它编译以下C++文件:
#include <Rcpp>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector plusTwo(NumericVector x){
NumericVector out = x + 2.0;
return out;
}
NumericVector giveOutput(NumericVector a){
NumericVector b = plusTwo(a);
return b;
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,Rcpp预处理器都plusTwo()可用,而giveOutput()不是.我能够找到的文档说这是一个应该创建一个包的点,但是在阅读了包后,它看起来比我需要的要复杂一个数量级.
如果没有明确定义plusTwo()内部giveOutput(),我该怎么办?
我正在阅读"使用Rcpp进行无缝R和C++集成"的第4章,我遇到了一些问题.
在"列表4.13"中,本书给出了一个关于如何使用R函数的例子.我尝试使用其他函数(不同的例子)并且我获得了成功.我的代码在这里:
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::DataFrame myrandom(Rcpp::NumericVector x) {
int n = x.size();
Rcpp::NumericVector y1(n), y2(n), y3(n);
y1 = Rcpp::pexp(x,1.0,1,0);
y2 = Rcpp::pnorm(x,0.0,1.0,1,0);
y3 = Rcpp::ppois(x,3.0,1,0);
return Rcpp::DataFrame::create(Rcpp::Named("Exp") = y1,Rcpp::Named("Norm") = y2, Rcpp::Named("Pois") = y3);
}
sourceCpp("random.cpp")
myrandom(c(0.5,1))
Run Code Online (Sandbox Code Playgroud)
在这种情况下还可以,但是当我尝试使用Rcpp :: pt时,我没有成功.我的代码在这里.
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::DataFrame myrandom2(Rcpp::NumericVector x) {
int n = x.size();
Rcpp::NumericVector y1(n);
y1 = Rcpp::pt(x,3.0,0,1,0);
return Rcpp::DataFrame::create(Rcpp::Named("T") = y1);
}
sourceCpp("random2.cpp")
myrandom2(c(0.5,1))
/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/stats/nt.h: In function ‘Rcpp::stats::P2<RTYPE, NA, T> Rcpp::pt(const Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, double, …Run Code Online (Sandbox Code Playgroud) 我刚开始使用Rcpp并想知道是否存在Rcpp糖功能列表.在翻译我的一些缓慢代码到C/C的过程++我需要通过基础R的功能,如提供的功能match,tabulate和which.
根据Hadley的高级R书 match的实施,Rcpp-sugar插图列出了一些可用的功能,虽然它看起来并不全面.
我真正想知道的是:有没有办法在包装文件或其他地方找到什么甜蜜甜蜜的功能以及我必须为自己写的东西?对于任何其他R包我直接去,R> help(package = "Rcpp")但在这种情况下似乎没有多大帮助.