rcpp

Moving from sourceCpp to a package w/Rcpp

痞子三分冷 提交于 2019-11-30 03:42:08
I currently have a .cpp file that I can compile using sourceCpp() . As expected the corresponding R function is created and the code works as expected. Here it is: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector exampleOne(NumericVector vectorOne, NumericVector vectorTwo){ NumericVector outputVector = vectorOne + vectorTwo; return outputVector; } I am now converting my project over to a package using Rcpp . So I created the skeleton with rStudio and started looking at how to convert things over. In Hadley's excellent primer on Cpp , he says in section "Using Rcpp in a

How to change and set Rcpp compile arguments

早过忘川 提交于 2019-11-30 03:12:24
问题 I created a new Rcpp package (by using RStudio). This package contains a C++ function that is compiled by using the following compiler options: clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include" -fPIC -Wall -mtune=core2 -g -O2 -c RcppExports.cpp -o RcppExports.o I would like to change/set these arguments, for example remove -g

How should I count the number of unique rows in a 'binary' matrix?

南楼画角 提交于 2019-11-30 02:59:03
问题 Suppose I have a matrix whose entries are only 0 and 1 , e.g. set.seed(123) m <- matrix( sample(0:1, 10, TRUE), nrow=5 ) with sample output: [,1] [,2] [1,] 0 0 [2,] 1 1 [3,] 0 1 [4,] 1 1 [5,] 1 0 The matrix will have at most 20 columns, and will have many rows. I want a function, let's call it rowCounts , that returns: The number of times a particular row appears in the matrix, and The index of the first occurrence of that row. How might I solve this problem? 回答1: Building on Kevin's answer,

How could I speed up this Rcpp code?

大兔子大兔子 提交于 2019-11-29 23:48:56
问题 I had implemented a function in R which was long to run. I have succeeded in improving it in R but now I would like to speed it up more by using Rcpp package. I have created the following Rcpp code. Unfortunately, it takes approximately the same time to run as the R code. I would like thus to improve it. Has anyone an idea on how to improve this piece of code? Thanks a lot! #include <math.h> #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double kernelcpp(NumericVector a,

Error when building R package using roxygen2

不羁岁月 提交于 2019-11-29 18:40:03
问题 I have 2 files, Rfile.R and Cppfile.cpp. Contents in Cppfile.cpp: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] int CPPF(int k){return ++k;} Contents in Rfile.R: RF<-function(k){return(CPPF(k))} I want to build an R package based on the 2 files. I use the lastest versions of Rstudio and Roxygen2. I tried 3 ways to build the package with or without Roxygen2, and had different results: New Project->New Directory->R package->Type:Package w/Rcpp, add both Rfile.R and Cppfile.cpp as

Rcpp and default C++ compiler

时光毁灭记忆、已成空白 提交于 2019-11-29 18:38:20
问题 I have some strange troubles with Rcpp - it uses unpredictable C++ compiler. This question is somewhat similar to this question. I'm on OSX, I have 2 complilers - default clang and clang-omp with openmp support. Also I have following ~/.R/Makevars file (where I set up clang-omp as default compiler): CC=clang-omp CXX=clang-omp++ CFLAGS += -O3 -Wall -pipe -pedantic -std=gnu99 CXXFLAGS += -O3 -Wall -pipe -Wno-unused -pedantic -fopenmp The problem is that, the package I'm developing compiles with

Constructing 3D array in Rcpp

那年仲夏 提交于 2019-11-29 18:21:06
问题 I am trying to map a 1D array onto 3D array using provided list of dimensions. Here are my components: SEXP data; // my 1D array // I can initialise new 3D vector in the following way: NumericVector vector(Dimension(2, 2, 2); // or the following: NumericVector vector(data.begin(), data.end()); What I didn't figure out is how can I create a NumericVector that would have both my data and the desired dimensions. 回答1: It is doable, but a little painful. I guess a decent (and tested) contribution

Convert std::vector to Rcpp matrix

橙三吉。 提交于 2019-11-29 18:04:32
问题 This is an Rcpp conversion related Q. I'm looking to convert a long std::vector into a Rcpp matrix object, but want to know if there is an easy conversion format. Naturally, you could loop over each element and fill an empty Rcpp matrix, but this seems error prone and possibly needless if a more convenient approach is possible. The reason I'm asking is I would like to utilize OpenMP in some existing C++ code, but storing element in the Rcpp matrix object directly in an OpenMP loop doesn't

Rcpp - Use multiple C++ functions in file referenced by sourceCpp?

早过忘川 提交于 2019-11-29 17:50:54
问题 I hope this isn't too obvious, as I've searched all day and can't find the answer. Say I have the following R file: library(Rcpp) sourceCpp("cfile.cpp") giveOutput(c(1,2,3)) And it compiles the following C++ file: #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; } No matter what I try, the Rcpp preprocessor makes plusTwo

How to test Rcpp::CharacterVector elements for equality?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 17:49:35
问题 I am trying to write some simple Rcpp code examples. This is remarkably easy with the Rcpp and inline packages. But I am stumped on how to test whether two character elements for equality. The following example compares the first elements of two character vectors. But I can't get it to compile. What is the trick? library(Rcpp) library(inline) cCode <- ' Rcpp::CharacterVector cx(x); Rcpp::CharacterVector cy(y); Rcpp::LogicalVector r(1); r[0] = (cx[0] == cy[0]); return(r); ' cCharCompare <-