I tried Rcpp and inline from last night, and so far I really like it. But I'm actually new to C in general, and I can only do basic things, and it's hard for me to find help on the Internet on things like functions.
Something I worked on was a function that finds a minimum of a vector in a global environment. I figured it out:
library("inline") library("Rcpp") foo <- rnorm(100) bar <- cxxfunction( signature(), ' Environment e = Environment::global_env(); NumericVector foo = e["foo"]; int min; for (int i = 0; i < foo.size(); i++) { if ( foo[i] < foo[min] ) min = i; } return wrap(min+1); ', plugin = "Rcpp") bar()
But it seems like there should be an easier way to do this, and it is rather slower than which.max()
system.time(replicate(100000,bar())) user system elapsed 0.27 0.00 0.26 system.time(replicate(100000,which.min(foo))) user system elapsed 0.2 0.0 0.2
I donβt notice the underlying c++ or Rcpp that does this? And if so, where can I find a list of such functions?
I think this question is related to: Where can I learn how to write C code to speed up the slow functions of R?
but it differs in that Iβm not very interested in how to include c++ in R , but more on how and where to learn the basic c++ code that can be used in R
c ++ r rcpp
Sacha epskamp
source share