我目前有一个.cpp可以使用的文件sourceCpp().正如所料,创建了相应的R函数,代码按预期工作.
这里是:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector exampleOne(NumericVector vectorOne, NumericVector vectorTwo){
NumericVector outputVector = vectorOne + vectorTwo;
return outputVector;
}
Run Code Online (Sandbox Code Playgroud)
我现在将我的项目转换为使用的包Rcpp.所以我用rStudio创建了骨架,并开始研究如何将事物转换过来.
在Hadley 关于Cpp的优秀入门书中,他在"在一个包中使用Rcpp"一节中说:
如果您的包使用Rcpp :: export属性,则需要在包构建过程中执行一个额外步骤.compileAttributes函数扫描包中的源文件以获取Rcpp :: export属性,并生成将函数导出到R所需的代码.
每当添加,删除函数或更改其签名时,都应重新运行compileAttributes.请注意,如果使用RStudio或devtools构建程序包,则会自动执行此步骤.
所以看起来编译的代码sourceCpp()应该像包中一样工作.
我创建了相应的R文件.
exampleOne <- function(vectorOne, vectorTwo){
outToR <- .Call("exampleOne", vectorOne, vectorTwo, PACKAGE ="testPackage")
outToR
}
Run Code Online (Sandbox Code Playgroud)
然后我(重新)构建了包,我得到了这个错误:
Error in .Call("exampleOne", vectorOne, vectorTwo, PACKAGE = "voteR") :
C symbol name "exampleOne" not in DLL for package "testPackage"
有没有人知道在使用sourceCpp()编译然后在包中使用它的代码时我还需要做什么?
我应该注意到我已经读过:"编写一个使用Rcpp的软件包" …
我正在使用Rstudio创建一个包,并探索使用Rcpp包来获取对C++代码的访问,但是,在尝试构建包时,错误被抛出如下:
fatal error: Rcpp.h: No such file or directory
内联C++代码编译很好,它只在考虑文件src夹中的独立C++文件时,显然是指文件#include <Rcpp.h>头部的指令.cpp.
我认为它可能与环境变量有关,有谁知道正确的配置是什么以及如何修复在Ubuntu 12.04 LTS环境中运行的Rstudio?
这些命令sourceCpp('./src/xyz.cpp')按预期Build and Reload执行,从RStudio IDE中执行时抛出错误.
我遇到了一个简单的Rcpp设置问题,我无法让它工作.我试着按照这个例子http://www.r-bloggers.com/user2013-the-rcpp-tutorial/ 但是在执行这段代码时:
library(Rcpp)
evalCpp("1 + 1", showOutput= TRUE)
Run Code Online (Sandbox Code Playgroud)
我得到了这个输出
C:/R/R-30~1.1/bin/x64/R CMD SHLIB -o "sourceCpp_33280.dll" "file8d01b0a675b.cpp"
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
WARNING: Rtools is required to build R packages but is not currently installed.
Please download and install the appropriate version of Rtools before proceeding:
Run Code Online (Sandbox Code Playgroud)
为了使它工作,我做了以下事情:
大多数类似的问题似乎表明文件路径中有一个空格,因此我几乎可以移动所有内容.但仍无法找到Rtools.我在我的笔记本电脑和桌面上试了一下它们都没有用,所以可能有一些结构我做错了.
我想复制以下R函数Rcpp:
fR = function(x) x[1:2]
fR(c(1,2,3))
#[1] 1 2
fR(c('a','b','c'))
#[1] "a" "b"
Run Code Online (Sandbox Code Playgroud)
我可以这样做固定输出类型,如下所示:
library(inline)
library(Rcpp)
fint = cxxfunction(signature(x = "SEXP"), '
List xin(x);
IntegerVector xout;
for (int i = 0; i < 2; ++i) xout.push_back(xin[i]);
return xout;', plugin = "Rcpp")
Run Code Online (Sandbox Code Playgroud)
但是这只适用于整数,如果我尝试用(或者,它们是相同的)替换xout类型- 它适用于任何输入类型,但我得到一个而不是一个向量.ListGenericVectorlist
Rcpp这样做的正确方法是什么?
我正在将基于R的代码转换为基于Rcpp的代码.我的职责是:
NumericMatrix createMatrixOfLinkRatiosC(NumericMatrix matr, double threshold4Clean) {
int i,j;
NumericMatrix myMatr(matr.nrow(),matr.ncol());
myMatr=matr;
....;
}
Run Code Online (Sandbox Code Playgroud)
我想处理对threshold4Clean缺失的函数的调用,但我没有找到怎么办...任何帮助将不胜感激.
我希望我的重写问题现在符合Stackoverflow的标准.请考虑以下示例.我正在编写一个Log-Likelihood函数,其中计算cdf over vectors是最耗时的部分.示例1使用R::pnorm,示例2近似于正常cdf erfc.正如您所看到的结果非常相似,ercf版本更快一点.
实际上(在MLE中)然而事实证明,ercf并不精确,这使得算法可以进入inf区域,除非准确地设置约束.我的问题:
1)我错过了什么吗?是否有必要实现一些错误处理(对于erfc)?
2)您是否有任何其他建议来加快代码或替代方案?考虑并行化for循环是否有回报?
require(Rcpp)
require(RcppArmadillo)
require(microbenchmark)
#Example 1 : standard R::pnorm
src1 <- '
NumericVector ppnorm(const arma::vec& x,const arma::vec& mu,const arma::vec& sigma, int lt, int lg) {
int n = x.size();
arma::vec res(n);
for (int i=0; i<n; i++) {
res(i) = R::pnorm(x(i),mu(i),sigma(i),lt,lg);
}
return wrap(res);
}
'
#Example 2: approximation with ercf
src2 <- '
NumericVector ppnorm(const arma::vec& x,const arma::vec& mu,const arma::vec& sigma, int lt, int lg) {
int n = x.size();
arma::vec …Run Code Online (Sandbox Code Playgroud) 考虑一个x在min和之间有界的有序向量max.下面是这样一个例子x,其中min可能是0与max可能是12:
x = c(0.012, 1, exp(1), exp(1)+1e-55, exp(1)+1e-10,
exp(1)+1e-3, 3.3, 3.33333, 3.333333333333333, 3+1/3, 5, 5, 10, 12)
Run Code Online (Sandbox Code Playgroud)
5和5以及exp(1)和exp(1)+10^(-55)具有完全相同的值(到浮点数的精度水平).其他一些条目差别很大,其他一些条款只有少量不同.我想考虑一下等式测试的近似值
ApproxEqual = function(a,b) abs(a-b) < epsilon
Run Code Online (Sandbox Code Playgroud)
,例如,epsilon可能在哪里1e-5.
目标
我想修改变量的值x"尽可能少",以确保没有两个值x"近似相等"并且x仍然在min和之间max.
我很高兴让你决定"尽可能少"的意思.例如,可以最小化原始x变量输出和预期变量输出之间的平方偏差之和.
例1
x_input = c(5, 5.1, 5.1, 5.1, 5.2)
min=1
max=100
x_output = c(5, …Run Code Online (Sandbox Code Playgroud) 我受到了fst包的启发,试图编写一个C++函数来快速序列化我在R到磁盘中的一些数据结构.
但即使在非常简单的对象上,我也无法达到相同的写入速度.下面的代码是一个将大的1 GB向量写入磁盘的简单示例.
使用自定义C++代码,我实现了135 MB/s的写入速度,这是我根据CrystalBench的磁盘限制.
在相同的数据上,write_fst实现了223 MB/s的写入速度,这似乎是不可能的,因为我的磁盘无法快速写入.(注意,我正在使用fst::threads_fst(1)和compress=0设置,并且文件具有相同的数据大小.)
我错过了什么?
如何让C++函数更快地写入磁盘?
C++代码:
#include <Rcpp.h>
#include <fstream>
#include <cstring>
#include <iostream>
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
// [[Rcpp::export]]
void test(SEXP x) {
char* d = reinterpret_cast<char*>(REAL(x));
long dl = Rf_xlength(x) * 8;
std::ofstream OutFile;
OutFile.open("/tmp/test.raw", std::ios::out | std::ios::binary);
OutFile.write(d, dl);
OutFile.close();
}
Run Code Online (Sandbox Code Playgroud)
R代码:
library(microbenchmark)
library(Rcpp)
library(dplyr)
library(fst)
fst::threads_fst(1)
sourceCpp("test.cpp")
x <- runif(134217728) # 1 gigabyte
df <- data.frame(x)
microbenchmark(test(x), write_fst(df, "/tmp/test.fst", compress=0), times=3)
Unit: seconds …Run Code Online (Sandbox Code Playgroud) 我从另一个问题中拿了这个例子.我正在用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)