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
19
votes
2 answers

rcpp function calling another rcpp function

I'm guessing this is an easy question, but I'm new to Cpp, and am stuck. I've created a function in R, using Rcpp and: // [[Rcpp::export]] I can call the function in R and it works as intended. Let's call it F1. Next, I want to create another…
Scott
  • 316
  • 3
  • 9
18
votes
5 answers

Rcpp Rtools installed but error message g++ not found

I have consulted existing entries on SO related to my specific issue, but still could not resolve it. I am trying to do this with my machine at work, where I have limited admin rights, but I can run Rtools.exe, so I installed it. My setup for R is: …
DavidH
  • 619
  • 2
  • 8
  • 15
18
votes
4 answers

How to use Rcpp to speed up a for loop?

I have created a for loop and I would like to speed it up using the Rcpp library. I am not too familiar with C++. Can you please help me make my function faster? Thank you for your help! I have included my algorithm, the code, along with the input…
user3602239
  • 671
  • 5
  • 15
18
votes
3 answers

Within C++ functions, how are Rcpp objects passed to other functions (by reference or by copy)?

I've just finished writing a new version of the ABCoptim package using Rcpp. With around 30x speed ups, I'm very happy with the new version's performance (vs old version), but I'm still having some concerns on if I have space to improve performance…
gvegayon
  • 812
  • 12
  • 23
18
votes
2 answers

Convert std::vector to Rcpp matrix

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…
philchalmers
  • 727
  • 7
  • 19
17
votes
2 answers

Cannot compile R packages with c++ code after updating to macOS Catalina

I just updated to macOS Catalina and tried to compile an R package I am developing. However, now it does not work. I am not sure it is because of the OS or because of the Rcpp package. Any help would be appreciated! > devtools::load_all(".") Loading…
Daijiang Li
  • 697
  • 4
  • 16
17
votes
3 answers

Efficiency of matrix rowSums() vs. colSums() in R vs Rcpp vs Armadillo

Background Coming from R programming, I'm in the process of expanding to compiled code in the form of C/C++ with Rcpp. As a hands on exercise on the effect of loop interchange (and just C/C++ in general), I implemented equivalents to R's rowSums()…
Mikko Marttila
  • 10,972
  • 18
  • 31
17
votes
2 answers

Deciding between NumericVector and arma::vec in Rcpp

With RcppArmadillo the conversion from R to Rcpp with arma::vec is just as easy as with Rcpp and NumericVector. My project utilizes RcppArmadillo. I'm unsure what to use, NumericVector or arma::vec? What are the key differences between those two?…
Clawish
  • 2,934
  • 3
  • 24
  • 28
17
votes
2 answers

Efficient weighted covariances in RcppEigen

I am trying to produce a function that can compute a series of weighted products where W is a diagonal matrix. There are many W matrices but only a single X matrix. To be efficient I can represent W as an array (w) containing the diagonal part.…
JCWong
  • 1,163
  • 1
  • 10
  • 29
17
votes
2 answers

A C++ version of the %in% operator in R

Is there any function in C++ equivalent to %in% operator in R? Consider the command below in R: which(y %in% x) I tried to find something equivalent in C++ (specifically in Armadillo) and I couldn't find anything. I then wrote my own function which…
Sam
  • 4,357
  • 6
  • 36
  • 60
17
votes
1 answer

Rcpp can't find Rtools: "Error 1 occurred building shared library"

I am running into a simple setup problem with Rcpp and I cannot get it to work. I tried to follow this example http://www.r-bloggers.com/user2013-the-rcpp-tutorial/ But when executing this code: library(Rcpp) evalCpp("1 + 1", showOutput= TRUE) I…
Timo Koole
  • 171
  • 1
  • 2
  • 3
16
votes
3 answers

How to test Rcpp::CharacterVector elements for equality?

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…
Andrie
  • 176,377
  • 47
  • 447
  • 496
16
votes
3 answers

Using Rcpp within parallel code via snow to make a cluster

I've written a function in Rcpp and compiled it with inline. Now, I want to run it in parallel on different cores, but I'm getting a strange error. Here's a minimal example, where the function funCPP1 can be compiled and runs well by itself, but…
Vincent
  • 15,809
  • 7
  • 37
  • 39
16
votes
3 answers

Difference between R's sum() and Armadillo's accu()

There are small differences in the results of R's sum() function and RcppArmadillo's accu() function when given the same input. For example, the following code: R: vec <- runif(100, 0, 0.00001) accu(vec) sum(vec) C++: //…
Matthew Lueder
  • 953
  • 2
  • 12
  • 25
16
votes
2 answers

Convert C array pointers to Rcpp with call by reference in R

I have the following codes in C. I am new to Rcpp and I want to convert the C codes I have to Rcpp. C code: #include #include #include void calculate(const double *d, double *w, int col, int x) { int i,j; for (i…
aliocee
  • 730
  • 9
  • 25