rcpp

Converting element of 'const Rcpp::CharacterVector&' to 'std::string'

主宰稳场 提交于 2019-12-06 12:42:23
I am wondering if there is a Rcpp way to convert an element or iterator of const CharacterVector& to std::string . If I try the following code void as(const CharacterVector& src) { std::string glue; for(int i = 0;i < src.size();i++) { glue.assign(src[i]); } } a compiler-time error will occurred: no known conversion for argument 1 from ‘const type {aka SEXPREC* const}’ to ‘const char*’ So far, I use C API to do the conversion: glue.assign(CHAR(STRING_ELT(src.asSexp(), i))); My Rcpp version is 0.10.2. By the way, I do know there is a Rcpp::as . glue.assign(Rcpp::as<std::string>(src[i])); the

R package with c++ code failed installation, No DLL was created

感情迁移 提交于 2019-12-06 12:22:53
I'm currently working on an R package which uses C++ code and includes external libraries (dlib, boost and an optimization library developed in the group). We're using Rcpp to integrate R and C++, but the problem is the package always fails to compile, and none of the similar questions I've found out there have worked for me. The report generated by R CMD check is: * installing *source* package 'IRTppExperimental' ... ** libs *** arch - i386 c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:

Rcpp: Mac shows loading wheel and almost freeze

十年热恋 提交于 2019-12-06 12:09:44
I created a R package which depends on Rcpp. A function in this package supposed to show printing statements at every n iterations. So I expect to see a new line on R console every few seconds. The odd thing is that when I run my function in R GUI, the cursor becomes a loading wheel and R "almost" freezes. The loading wheel disappear once after the computation is done. The minimal example of this situation is summarized as follow: library(inline) library(Rcpp) test <- cxxfunction( signature(), body= ' RNGScope scope; for (int i = 0; i < 100; i++) { sleep(1); // sleep one second at each

Multiplying a column vector by a numeric scalar in RcppArmadillo

本小妞迷上赌 提交于 2019-12-06 11:47:45
问题 I am having some trouble compiling this simple c++ code using Rcpp and the RcppArmadillo package. Take the following simple example to multiply each column of a matrix by a numeric scalar: code <- 'arma::mat out = Rcpp::as<arma::mat>(m); for(int i = 0; i < out.n_cols; ++i){ out.col(i) *= v; } return Rcpp::wrap( out );' Trying to compile this using... require( RcppArmadillo ) armMult <- cxxfunction( signature( m = "numeric" , v = "numeric" ), code , plugin = "RcppArmadillo" ) Results in the

Extract long[] from R object

二次信任 提交于 2019-12-06 11:07:50
I'm trying to make a wrapper for some C-based sparse-matrix-handling code (see previous question ). In order to call the workhorse C function, I need to create a structure that looks like this: struct smat { long rows; long cols; long vals; /* Total non-zero entries. */ long *pointr; /* For each col (plus 1), index of first non-zero entry. */ long *rowind; /* For each nz entry, the row index. */ double *value; /* For each nz entry, the value. */ }; These correspond nicely to the slots in a dgCMatrix sparse matrix. So ideally I'd just point to the internal arrays in the dgCMatrix (after

R package with both .c and .cpp files with Rcpp

女生的网名这么多〃 提交于 2019-12-06 10:16:22
I'm trying to build an R package which contains both C (in the form of .c files) and C++ code (in the form of .cpp files) using the Rcpp package as a dependency. I have a couple of questions. First, is it actually possible to do this? Can one call C scripts and C++ scripts that are in the same R package? If the previous is possible, how then does one properly register the functions in the C and C++ scripts. To help with this, I have set up a little example which is available on my GitHub page ( https://github.com/tpbilton/testrcpp ). I have used Rcpp.package.skeleton("testrcpp") to initialize

How to slice Rcpp NumericVector for elements 2 to 101?

删除回忆录丶 提交于 2019-12-06 08:37:10
问题 Hi I'm trying to slice Rcpp's NumericVector for elements 2 to 101 in R, I would do this: array[2:101] How do I do the same in RCpp? I tried looking here: http://gallery.rcpp.org/articles/subsetting/ But the resource has an example that lists all the elements using IntegerVector::create(). However, ::create() is limited by the number of elements. (in addition to being tedious). Any way to slice a vector given 2 indices? 回答1: This is possible with Rcpp 's Range function. This generates the

Link the R Package Depending on RcppEigen with MKL in Microsoft R Open

[亡魂溺海] 提交于 2019-12-06 08:11:03
问题 I have built a custom package with some functions written in RcppEigen. I also have Microsoft R open with Intel MKL enabled. How could I link the R package to the Intel MKL feature? Setup 1 : Below are procedures that I have tried to link the package with MKL in the normal R, but failed: The Eigen documents says I need: 1. #define EIGEN_USE_MKL_ALL 2. link your program to MKL libraries (the MKL linking advisor) Based on 2, in my file Makevars PKG_CXXFLAGS = -I/opt/intel/mkl/include PKG_LIBS =

Understanding `Makevars` for linking to external C library in R package

陌路散爱 提交于 2019-12-06 07:42:14
问题 I am working on a package which includes C code from third-party library (SUNDIALS). The package compiles and works (i.e., is able to solve a test ODE) with the following Makevars file performing static linking CXX=clang++ PKG_CPPFLAGS = -I../inst/include PKG_LDFLAGS = /usr/local/lib PKG_LIBS= $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) $(PKG_LDFLAGS)/libsundials_cvode.a $(PKG_LDFLAGS)/libsundials_nvecserial.a However, a slightly modified version (based on the example in R-Exts, i.e. - PKG_LIBS = -L

Why my Rcpp code is not much faster?

佐手、 提交于 2019-12-06 06:27:33
问题 For practicing my C++, I'm trying to convert some R code to Rcpp. The code is a greedy algorithm implemented in this answer. Next, see my Rcpp code (in a .cpp file), and some benchmark of the two codes: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] List create_groups2(const NumericVector& x, double thr) { int n = x.size(); List res(n); int c = 0; double sum; std::list<double> x2(n); std::copy(x.begin(), x.end(), x2.begin()); // copy x in x2 x2.sort(std::greater<double>()); //