rcpp

Running compiled C++ code with Rcpp

谁说我不能喝 提交于 2019-12-03 12:44:46
问题 I have been working my way through Dirk Eddelbuettel's Rcpp tutorial here: http://www.rinfinance.com/agenda/ I have learned how to save a C++ file in a directory and call it and run it from within R. The C++ file I am running is called 'logabs2.ccp' and its contents are directly from one of Dirk's slides: #include <Rcpp.h> using namespace Rcpp; inline double f(double x) { return ::log(::fabs(x)); } // [[Rcpp::export]] std::vector<double> logabs2(std::vector<double> x) { std::transform(x.begin

Best way to use c++ code from R package FOO in package BAR

感情迁移 提交于 2019-12-03 12:42:44
问题 I am trying to define a function using Rcpp for speedup. The situation is as follows: I have a package FOO with a lot of C++ code (my own package and currently not using Rcpp) which have defined a set of functions e.g. foo_a and foo_b. In another package BAR (using Rcpp) I am defining a function (using Rcpp Attributes) where I want to call functions foo_a and foo_b. How do I solve this? Looking a bit in other posts I get that I have in some way to include header files in FOO and use attribute

How to resize a NumericVector?

谁说我不能喝 提交于 2019-12-03 11:10:27
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 copy-constructor to get the correct size of the NumericVector . This is in such a case much faster than usage of push_back ) If you prefer the C++ idioms, use std::vector<double> and return that at the end where it will be converted via an implicit wrap() to an R vector. You could also use Armadillo or Eigen vectors via RcppArmadillo and RcppEigen. Our objects

Deciding between NumericVector and arma::vec in Rcpp

谁说我不能喝 提交于 2019-12-03 10:42:39
问题 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? When to use which? Is there a performance/memory advantage of using one over the other? Are the only difference the member functions? And, as a bonus question: should I even consider arma::colvec or arma::rowvec ? 回答1: What are the key differences

Rcpp Rcpp.package.skeleton(“mypackage”) “rcpp_hello_world” not available for .Call() for package “mypackage”

做~自己de王妃 提交于 2019-12-03 10:12:44
I've managed to get the Rcpp.package.skeleton to INSTALL in Windows by the following commands at the R prompt - Rcpp.package.skeleton("mypackage") system("R CMD build mypackage") system("R CMD INSTALL mypackage") library(mypackage) This creates the mypackage.dll . However when I do the following commands - rcpp_hello_world <- function(){ .Call( "rcpp_hello_world", PACKAGE = "mypackage")} rcpp_hello_world() I get the following error: Error in .Call("rcpp_hello_world", PACKAGE = "mypackage") : "rcpp_hello_world" not available for .Call() for package "mypackage" I run sessionInfo() and I get the

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

家住魔仙堡 提交于 2019-12-03 09:01:06
问题 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 is very slow compared to the R command above. Here is what I wrote: #include <RcppArmadillo.h> // [[Rcpp::depends("RcppArmadillo")]] // [[Rcpp::export]] arma::uvec myInOperator(arma::vec myBigVec, arma::vec mySmallVec ){ arma::uvec rslt = find

R: dplyr solution for for-loop with initial conditions set

徘徊边缘 提交于 2019-12-03 09:00:31
I have a data which has 40 days of the year and some data set.seed(123) df <- data.frame(day = 1:40,rain = runif(40,min = 0, max = 3), petc = runif(40, min = 0.3, max = 8),swc = runif(40, min = 27.01, max = 117.43)) I want to calculate another variable called aetc for each day which is calculated as follows: SW.ini <- 2 # setting some initial values SW.max <- 5 SW.min <- 0 For day 1, 1) Determine a variable called PAW(day1) = SW.ini + rain(day1) 2) If PAW(day1) >= SWC(day1), aetc(day1) = petc(day1) ; If `PAW(day1) < SWC(day1), aetc(day1) = PAW(day1)/SWC(day1) * petc(day1)` 3) Check if aetc

Elementwise matrix multiplication: R versus Rcpp (How to speed this code up?)

亡梦爱人 提交于 2019-12-03 07:52:33
问题 I am new to C++ programming (using Rcpp for seamless integration into R ), and I would appreciate some advice on how to speed up some calculations. Consider the following example: testmat <- matrix(1:9, nrow=3) testvec <- 1:3 testmat*testvec # [,1] [,2] [,3] #[1,] 1 4 7 #[2,] 4 10 16 #[3,] 9 18 27 Here, R recycled testvec so that, loosely speaking, testvec "became" a matrix of the same dimensions as testmat for the purpose of this multiplication. Then the Hadamard product is returned. I wish

How to use Rcpp to speed up a for loop?

谁说胖子不能爱 提交于 2019-12-03 06:50:13
问题 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 and the output, with the sessionInfo. Here is my algorithm: if the current price is above the previous price, mark (+1) in the column named TR if the current price is below the previous price, mark (-1) in the column named TR if the current price is

Link error installing Rcpp “library not found for -lintl”

前提是你 提交于 2019-12-03 06:17:40
I just stumbled over a linker error when trying to install some R packages which have Rcpp as a dependency. My setup is Mac OS X 10.9.1 (Mavericks), R 3.0.2 installed by Homebrew. Here's the error output: > install.packages('Rcpp') trying URL 'http://cran.fhcrc.org/src/contrib/Rcpp_0.10.6.tar.gz' Content type 'application/x-gzip' length 1985569 bytes (1.9 Mb) opened URL ================================================== downloaded 1.9 Mb * installing *source* package ‘Rcpp’ ... ** package ‘Rcpp’ successfully unpacked and MD5 sums checked ** libs clang++ -I/usr/local/Cellar/r/3.0.2/R.framework