rcpp

Create two R functions with same name but different type of argument

柔情痞子 提交于 2020-01-13 08:37:06
问题 I am creating a package with Rcpp and I want to have a function than can accept as argument a std::string or a int . How can I do it? I have the following functions: int myFunction(std::string varname); RcppExport SEXP myFunction(SEXP varname) { BEGIN_RCPP Rcpp::RObject __result; Rcpp::RNGScope __rngScope; std::string myVarname = as<std::string>(varname); __result = Rcpp::wrap(myFunction(myVarname)); return __result; END_RCPP } int myFunction(int varname); RcppExport SEXP myFunction(SEXP

Template for class returning elements of Rcpp::Vector<RTYPE>

本小妞迷上赌 提交于 2020-01-13 06:56:07
问题 Consider the following class that does absolutely nothing: class MyNumVec { private: const NumericVector& x; public: MyNumVec(const NumericVector& y) : x(y) {} double operator[](int i) const { // here return x[i]; } operator NumericVector() const { return x; } }; I would like to make it more general and use template for it so that I'll be working with any Vector<RTYPE> instead of numeric vectors only, but the problem is the line marked by comment since I must declare output type. I tried

Indexing slice from 3D Rcpp NumericVector

↘锁芯ラ 提交于 2020-01-13 06:42:24
问题 Hi I have what I think must be a really simple Rcpp question regarding treating NumericVector objects as multidimensional arrays. I can't find an answer to what might be obvious. Apologies up front if this is the case -- my inexperience with C++ is to blame... If I use the answer posted here a (Constructing 3D array in Rcpp) as an example library("Rcpp") cppFunction(code=' NumericVector arrayC(NumericVector input, IntegerVector dim) { input.attr("dim") = dim; return input; } ') How do I

Paste the elements of two columns [duplicate]

孤街浪徒 提交于 2020-01-11 12:26:07
问题 This question already has answers here : Speedy/elegant way to unite many pairs of columns (3 answers) Closed 4 years ago . I have a data.frame of the following kind set.seed(12) d = data.frame(a=sample(5,x=1:9), b=sample(5,x=1:9), c=sample(5,x=1:9), d=sample(5,x=1:9), e=sample(5,x=1:9), f=sample(5,x=1:9)) d # a b c d e f # 1 1 1 4 4 2 3 # 2 7 2 7 9 7 5 # 3 8 5 3 8 1 2 # 4 2 9 8 7 5 9 # 5 9 6 2 1 9 4 I would like to take the first two columns, convert the integer into characters and paste the

How do I compile a dll with R and RCPP?

匆匆过客 提交于 2020-01-10 04:16:24
问题 I have written a .cpp file and I want to compile it to a .dll for use with R and RCPP. (without using the inline package). I am using WinXP and R 2.13.2 and RCPP 0.9.7. I am using Rtools 2.14. How do I include Rcpp.h in minGW's search path? I underastand that the files that I need to include are in C:\Program Files\R\R-2.13.2\library\Rcpp\include. However, I was not able to add the to "add" them to "search path". I tired a temporary "hack". I copied the contents of C:\Program Files\R\R-2.13.2

Building a tiny R package with CUDA and Rcpp

∥☆過路亽.° 提交于 2020-01-09 09:51:10
问题 I'm working on a tiny R package that uses CUDA and Rcpp, adapted from the output of Rcpp.package.skeleton() . I will first describe what happens on the master branch for the commit entitled "fixed namespace". The package installs successfully if I forget CUDA (i.e., if I remove the src/Makefile, change src/rcppcuda.cu to src/rcppcuda.cpp, and comment out the code that defines and calls kernels). But as is, the compilation fails. I also would like to know how to compile with a Makevars or

Building a tiny R package with CUDA and Rcpp

て烟熏妆下的殇ゞ 提交于 2020-01-09 09:51:09
问题 I'm working on a tiny R package that uses CUDA and Rcpp, adapted from the output of Rcpp.package.skeleton() . I will first describe what happens on the master branch for the commit entitled "fixed namespace". The package installs successfully if I forget CUDA (i.e., if I remove the src/Makefile, change src/rcppcuda.cu to src/rcppcuda.cpp, and comment out the code that defines and calls kernels). But as is, the compilation fails. I also would like to know how to compile with a Makevars or

Rcpp programming efficiency

非 Y 不嫁゛ 提交于 2020-01-07 09:03:11
问题 I am a beginner with Rcpp. Currently I wrote a Rcpp code, which was applied on two 3 dimensional arrays: Array1 and Array2 . Suppose Array1 has dimension (1000, 100, 40) and Array2 has dimension (1000, 96, 40). I would like to perform wilcox.test using: wilcox.test(Array1[i, j,], Array2[i,,]) In R , I wrote nested for loops that completed the calculation in about a half hour. Then, I wrote it into Rcpp. The calculation within Rcpp took an hour to achieve the same results. I thought it should

How to convert a R function into a C++ function using Rcpp?

让人想犯罪 __ 提交于 2020-01-06 10:10:46
问题 I have defined the following function: pij = function(vec){ out = vec %*% t(vec) diag(out) = NA out = sum(out, na.rm = T) return(out) } where vec is a vector, for instance vec = rnorm(10^4,0,1) . I would like to know how this function can be written in C++ using the Rcpp package. 回答1: I would suggest thinking about the math behind the problem first. For a vector v you are trying to calculate sum_{i=1}^{N-1} sum_{j=i+1}^{N} 2 * v_i * v_j You can do that by creating the matrix v_i * v_j first,

How to convert a R function into a C++ function using Rcpp?

人走茶凉 提交于 2020-01-06 10:10:26
问题 I have defined the following function: pij = function(vec){ out = vec %*% t(vec) diag(out) = NA out = sum(out, na.rm = T) return(out) } where vec is a vector, for instance vec = rnorm(10^4,0,1) . I would like to know how this function can be written in C++ using the Rcpp package. 回答1: I would suggest thinking about the math behind the problem first. For a vector v you are trying to calculate sum_{i=1}^{N-1} sum_{j=i+1}^{N} 2 * v_i * v_j You can do that by creating the matrix v_i * v_j first,