Questions tagged [rcpp]

Rcpp provides seamless integration of C++ code in R.

Rcpp is an package allowing integration with code. R data types (SEXP) are matched to C++ objects in a class hierarchy. All R types are supported and each type is mapped to a dedicated class.

Repositories

Vignettes

Books

Other resources

Related tags

3011 questions
16
votes
1 answer

Using C++ libraries in an R package

What is the best way to make use of a C++ library in R, hopefully preserving the C++ data structures. I'm not at all a C++ user, so I'm not clear on the relative merits of the available approaches. The R-ext manual seems to suggest wrapping every…
Peter
  • 1,155
  • 8
  • 20
16
votes
4 answers

Error when compiling Rcpp code in an R package using RStudio

I am using Rstudio to create a package, and exploring the use of the Rcpp package to gain access to C++ code, however, when trying to build the package, and error is being thrown as follows: fatal error: Rcpp.h: No such file or directory Inline C++…
Nicholas Hamilton
  • 10,044
  • 6
  • 57
  • 88
15
votes
3 answers

Rcpp function check if missing value

I'm converting R based code into Rcpp based code. The head of my function is: NumericMatrix createMatrixOfLinkRatiosC(NumericMatrix matr, double threshold4Clean) { int i,j; NumericMatrix myMatr(matr.nrow(),matr.ncol()); myMatr=matr; ....; } I…
Giorgio Spedicato
  • 2,413
  • 3
  • 31
  • 45
15
votes
2 answers

passing unevaluated expressions to C/C++

I'd like to pass a variable number of arguments from a function to C/C++, but would like to leave the arguments unevaluated and at the same time don't want to do any computations in R (aside from calling the C/C++ function), i.e. I don't want to…
eddi
  • 49,088
  • 6
  • 104
  • 155
15
votes
3 answers

Error when with Xcode 5.0 and Rcpp (Command Line Tools ARE installed)

I have a new iMac and I'm trying to run code using the Rcpp library that has been working on both my old iMac and Macbook Pro without issue. I have tried everything I can't seem to figure out what the issue is. Xcode 5.0 downloaded. Command line…
user2904609
  • 151
  • 1
  • 1
  • 3
15
votes
3 answers

Moving from sourceCpp to a package w/Rcpp

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 using namespace Rcpp; // [[Rcpp::export]] NumericVector…
politicalEconomist
  • 1,041
  • 1
  • 14
  • 19
15
votes
1 answer

Rcpp pass by reference vs. by value

I made a first stab at an Rcpp function via inline and it solved my speed problem (thanks Dirk!): Replace negative values by zero The initial version looked like this: library(inline) cpp_if_src <- ' Rcpp::NumericVector xa(a); int n_xa =…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
14
votes
2 answers

1-dimensional Matrix is changed to a vector in R

> 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" In R,…
Jona
  • 629
  • 1
  • 6
  • 16
14
votes
1 answer

writing an object to disk in R through C++ vs. fst

I was inspired by the fst package to try to write a C++ function to quickly serialize some data structures I have in R to disk. But I am having trouble achieving the same write speed even on very simple objects. The code below is a simple example…
thc
  • 9,527
  • 1
  • 24
  • 39
14
votes
1 answer

How to use subfolders in 'src/' in R packages?

C or C++ code inside subfolders in the src/ for example src/libfoo don't get compiled when I install a package. When I searched on other questions I found this that mentions Makevars. I searched the Matrix package Makevars. I I thought that I…
Daniel Falbel
  • 1,721
  • 1
  • 21
  • 41
14
votes
3 answers

Matrix multiplication in Rcpp

First of all, I am a novice user so forget my general ignorance. I am looking for a faster alternative to the %*% operator in R. Even though older posts suggest the use of RcppArmadillo, I have tried for 2 hours to make RcppArmadillo work without…
David
  • 427
  • 3
  • 10
14
votes
1 answer

How to return R's NULL in Rcpp code?

Suppose I have a C++ code to compile with Rcpp and will be called in R. // [[Rcpp::export]] SEXP to_env(List x) { if(x.hasAttribute("names")) { return x; } else { return NULL; } } What should the NULL value be to return R's NULL…
Kun Ren
  • 4,715
  • 3
  • 35
  • 50
14
votes
2 answers

how can I handle vectors without knowing the type in Rcpp

I want to replicate the following R function in Rcpp: fR = function(x) x[1:2] fR(c(1,2,3)) #[1] 1 2 fR(c('a','b','c')) #[1] "a" "b" I can do it for a fixed output type like so: library(inline) library(Rcpp) fint = cxxfunction(signature(x =…
eddi
  • 49,088
  • 6
  • 104
  • 155
14
votes
3 answers

Convert RcppArmadillo vector to Rcpp vector

I am trying to convert RcppArmadillo vector (e.g. arma::colvec) to a Rcpp vector (NumericVector). I know I can first convert arma::colvec to SEXP and then convert SEXP to NumericVector (e.g. as(wrap(temp)), assuming temp is an…
Raymond Wong
  • 149
  • 1
  • 4
14
votes
1 answer

How to resize a NumericVector?

How can I resize in Rcpp a NumericVector? When I use the push_back function for this, the Program slows down. But there are no .resize() or .reserve() functions. (When I have already a NumericVector with the desired size, I can use the…
ktrask
  • 153
  • 1
  • 5