rcpp

RcppTN .cpp script works when sourced but not when compiled in library

ⅰ亾dé卋堺 提交于 2019-12-11 07:05:22
问题 I am trying to create a R package that uses random draws from a truncated normal in a .cpp script. I am using the rtn1 function from the pckage RcppTN. If I source the code, the function works fine. Once I build the package I get the error: > library(testtruncnorm) > testtruncnorm() Error in testtruncnorm::testtruncnorm() : function 'RcppTN_rtn1' not provided by package 'RcppTN' Simplified .cpp code is here #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] #include <RcppTN.h> //

set RNG state with openMP and Rcpp

北城以北 提交于 2019-12-11 06:48:21
问题 I have a clarification question. It is my understanding, that sourceCpp automatically passes on the RNG state, so that set.seed(123) gives me reproducible random numbers when calling Rcpp code. When compiling a package, I have to add a set RNG statement. Now how does this all work with openMP either in sourceCpp or within a package? Consider the following Rcpp code #include <Rcpp.h> #include <omp.h> // [[Rcpp::depends("RcppArmadillo")]] // [[Rcpp::export]] Rcpp::NumericVector rnormrcpp1(int n

Subset an atomic vector in-place

南笙酒味 提交于 2019-12-11 06:47:52
问题 Continuing from Subsetting a large vector uses unnecessarily large amounts of memory : Given an atomic vector, for example x <- rep_len(1:10, 1e7) How can I modify x in-place to remove elements by numeric index using Rcpp? In R, one can do this, but not in-place (i.e. without duplicating x ): idrops <- c(5, 4, 9) x <- x[-idrops] A reasonably efficient way to do this would be the following: IntegerVector dropElements(IntegerVector x, IntegerVector inds) { R_xlen_t n = x.length(); R_xlen_t

How do I run Rcpp Hello World?

徘徊边缘 提交于 2019-12-11 06:33:03
问题 OK, so I have created an R package foo with function Rcpp.package.skeleton . I have also compiled the Hello World C++ file with R CMD SHLIB foo/src/rcpp_hello_world.cpp However, when I call rcpp_hello_world I get an error: > source("foo/R/rcpp_hello_world.R") > rcpp_hello_world() Error in .Call("rcpp_hello_world", PACKAGE = "foo") : "rcpp_hello_world" not available for .Call() for package "foo" Any clues? 回答1: "Package skeleton" implies that you are supposed to follow the creation of a

RQuantlib and Mac OS X 10.8.2

爱⌒轻易说出口 提交于 2019-12-11 06:24:04
问题 I'm a total newbie in Mac OS X, R and C++. Sound like a good mix, doesn't it? I have the need to use RQuantLib, because I want to use some pricing functions part of the QuantLib package inside R, all on a Mac OS X-powered environment. I've correctly installed QuantLib. I've already asked to the official QuantLib mailing list, and together we seem to have reached the conclusion that the problems I'm encountering are not related to my QuantLib installation, which seems ok and correctly

Should SEXP function args be PROTECTed when put inside an Rcpp::Xptr?

痴心易碎 提交于 2019-12-11 06:13:53
问题 Look at the (oversimplified) Rcpp + R code below : test.cpp : #include <Rcpp.h> using namespace Rcpp; class VecWrap{ public: SEXP vector; int type; VecWrap(SEXP vector) { this->vector = vector; this->type = TYPEOF(vector); if(this->type != INTSXP && this->type != REALSXP) stop("invalid type"); } bool contains(double val){ if(type == INTSXP){ IntegerVector v = vector; for(int i = 0; i < v.size(); i++) if(v[i] == val) return true; }else if(type == REALSXP){ NumericVector v = vector; for(int i =

Using Rcpp Modules to expose C++ classes

你。 提交于 2019-12-11 05:49:36
问题 I am trying to use Rcpp Modules to expose C++ classes to R. I tried to create a simple example to understand how this works. I have two classes Bar and Foo that are stored in their own cpp files, in the src folder of the package. The code for Bar.cpp is the following: #include "Bar.h" #include "Foo.h" #include <Rcpp.h> using namespace Rcpp; Bar::Bar(){x = 0;}; int Bar::getX(){return x;} void Bar::setX(int num){x = num;} int Bar::sumXY(){ Foo f = Foo(); f.setY(5); return x + f.getY(); } RCPP

Rcpp: Install package with static libraries for platform independent usage

你离开我真会死。 提交于 2019-12-11 05:34:38
问题 I want to use the libDAI C++ library within an R-package and want the package: to be usable on Linux and Windows save disc space (the external library has ~60 Mb) the end user does not need to install boost and gmp for compilation My current setup is: precompile libDAI copy libdai.a to lib/ copy all libDAI header files to inst/include add Makevar to src/ Modify Makevar file: # include libraries PKG_CPPFLAGS =-I../inst/include/ PKG_LIBS = -Llib -l../lib/libdai.a My script for accessing the

Rcpp Eigen Map Error with MatrixXf

余生颓废 提交于 2019-12-11 05:34:08
问题 Why does the following code not compile? library(Rcpp) cppFunction(' int rows(const NumericMatrix& X) { using Eigen::MatrixXf; typedef Eigen::Map<MatrixXf> MapMat; MapMat X1(as<MapMat>(X)); return X1.rows(); }', depends = "RcppEigen") It throws the following error: error: no matching function for call to 'Eigen::Map<Eigen::Matrix<float, -1, -1> >::Map(Rcpp::Vector<14, Rcpp::PreserveStorage>::iterator, int&, int&)' OUT get() {return OUT(vec.begin(), d_nrow, d_ncol );} The same code works fine

R packages with Rcpp and nloptr

醉酒当歌 提交于 2019-12-11 05:17:54
问题 I have been building an r-package that runs RcppParallel and calls nloptr from the cpp in parallel. Currently, the package will not build as it can't find the 'nloptrAPI.h' file. The build log outputs: * checking dependencies in R code ... NOTE Namespaces in Imports field not imported from: ‘RcppArmadillo’ ‘nloptr’ My question is if there is a simple fix for this. Or if I would have to rewrite the function to call 'nlopt' from the cpp version and add a 'makevars' file to the package. The src