rcpp

Error when building R package using roxygen2

☆樱花仙子☆ 提交于 2019-11-30 13:52:34
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 source files. Build & reload, everything works out fine. The functions work as they do. New Project-

Constructing 3D array in Rcpp

拥有回忆 提交于 2019-11-30 13:41:49
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. It is doable, but a little painful. I guess a decent (and tested) contribution for new constructor or helper function would be appreciated. In the meantime, you can do what the example

Rcpp and default C++ compiler

浪子不回头ぞ 提交于 2019-11-30 13:12:48
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 clang++ , not clang-omp++ . I also tried (as experiment to lacate issue) to change package src

Convert RcppArmadillo vector to Rcpp vector

别来无恙 提交于 2019-11-30 12:50:17
问题 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<NumericVector>(wrap(temp) ), assuming temp is an arma::colvec object). But what is a good way to do that? I want to do that simply because I am unsure if it is okay to pass arma::colvec object as a parameter to an Rcpp::Function object. 回答1: I was trying to Evaluate a Rcpp::Function with argument

Convert std::vector to Rcpp matrix

人走茶凉 提交于 2019-11-30 12:50:07
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 appear to work well (whereas loading a std::vector object and converting to a matrix after the OpenMP loop

Profiling Rcpp code on OS X

廉价感情. 提交于 2019-11-30 11:54:21
问题 I am interested in profiling some Rcpp code under OS X (Mountain Lion 10.8.2), but the profiler crashes when being run. Toy example, using inline , just designed to take enough time for a profiler to notice. library(Rcpp) library(inline) src.cpp <- " RNGScope scope; int n = as<int>(n_); double x = 0.0; for ( int i = 0; i < n; i++ ) x += (unif_rand()-.5); return wrap(x);" src.c <- " int i, n = INTEGER(n_)[0]; double x = 0.0; GetRNGstate(); for ( i = 0; i < n; i++ ) x += (unif_rand()-.5);

How to test Rcpp::CharacterVector elements for equality?

℡╲_俬逩灬. 提交于 2019-11-30 11:49:26
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 <- cxxfunction(signature(x="character", y="character"), plugin="Rcpp", body=cCode) cCharCompare("a", "b") --

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

半城伤御伤魂 提交于 2019-11-30 11:38:42
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 get this output C:/R/R-30~1.1/bin/x64/R CMD SHLIB -o "sourceCpp_33280.dll" "file8d01b0a675b.cpp" Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, : Error 1 occurred building shared library. WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate

Adding an external library in R package using Rcpp

纵饮孤独 提交于 2019-11-30 10:11:56
I am trying to develop an R package which uses the Sundials C library for solving differential equations. In order to not have the user install the library, I am putting the source code of the library in my package. I have put all the header files from the library in /inst/include/sundials-2.6.2 and the .c files in src/sundials-2.6.2 of my package folder. From my reading of the SO posts on this topic, sourceCpp of code in multiple files (e.g., separate .h and .cpp files should work if they are structured to be a part of the package. I am trying to run a example code file from the Sundials

Converting an Armadillo Matrix to an Eigen MatriXd and vice versa

这一生的挚爱 提交于 2019-11-30 09:37:28
How can I convert from an Armadillo Matrix to an Eigen MatrixXd and vice versa? I have nu as an arma::vec of size N , z as arma::mat of dimension N x 3 . I want to compute a matrix P such as the entry P_ij is Pij=exp(nu(i) + nu(j) + z.row(j)*z.row(j))) Thus I used this code int N=z.n_rows; mat P= exp(nu*ones(1,N) + one(N,1)*(nu.t()) + z*(z.t())); But the computation takes too long. In particular, for N = 50,000 the run time is far to high. It seems that using Eigen can be faster. But my matrix are Armadillo. How can I use Eigen operations ? Or how can I do this operation faster. Using