rcpp

Rcpp and boost: it should work but it does not

荒凉一梦 提交于 2019-12-10 11:17:46
问题 I have a question about the interesting post found at: Rcpp with quad precision computation I use: // [[Rcpp::depends(BH)]] #include <Rcpp.h> #include <boost/multiprecision/float128.hpp> //#include <boost/multiprecision/mpfr.hpp> namespace mp = boost::multiprecision; // [[Rcpp::export]] std::string qexp(double da = -1500.0, double db = -1501.0) { mp::float128 a(da), b(db); mp::float128 res = mp::exp(a) / (mp::exp(a) + mp::exp(b)); return res.convert_to<std::string>(); } // // [[Rcpp::export]]

Rcpp Update matrix passed by reference and return the update in R

橙三吉。 提交于 2019-12-10 10:08:14
问题 I checked many examples about how to pass by reference using Rcpp. I see for instance this very great. However I have one question. Suppose that I have a matrix as an object in R and I want to add 1 to the entry [1,1]. The examples i saw work if the matrix is in Cpp but i want to return the update in R without using return statement. This is an example i did with a list and it works very well //[[Rcpp::export]] void test(List& a){ a(0)=0; } I need to do similarely with a matrix. something

R: pointer to c function

爱⌒轻易说出口 提交于 2019-12-10 06:44:40
问题 How to pass a pointer to a function from C code to R (using External R) and after call that function from R?? Something like: C: typedef void (* FunctionPtr)(); SEXP ans; PROTECT(ans = /* ?some code? */); R_tryEval(ans, R_GlobalEnv, NULL); UNPROTECT(1); R: callback_function() EDIT: @Romain Francois's post was very helpful. myapp code: namespace { void callback() { std::cout << "callback\n" << std::flush; } } class Worker { public: /*...*/ void initialize(argc, argv) { Rf_initEmbeddedR(argc,

Using Rcpp function in parLapply on Windows

徘徊边缘 提交于 2019-12-10 05:53:00
问题 I'm doing R code optimization with Rcpp and parallel computing on Windows. I have a trouble calling Rcpp function in parLapply. The example is following Rcpp code (test.cpp) #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector payoff( double strike, NumericVector data) { return pmax(data - strike, 0); } R code library(parallel) library(Rcpp) sourceCpp("test.cpp") strike_list <- as.list(seq(10, 100, by = 5)) data <- runif(10000) * 50 # One core version strike_payoff <-

Raising exceptions in Rcpp

跟風遠走 提交于 2019-12-10 03:30:20
问题 I am trying to report errors from my rcpp code. I am using the constructor exception (const char *message_, const char *file, int line) from http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.html. To isolate the problem, I wrote the following bar.cpp : #include <Rcpp.h> RcppExport SEXP bar( SEXP x){ throw(Rcpp::exception("My Error Message","bar.cpp",4)); return x ; } When I run it in R, this is what I get: > dyn.load("bar.so") > is.loaded("bar") [1] TRUE > .Call("bar",12)

store and retrieve matrices in memory using xptr

亡梦爱人 提交于 2019-12-10 03:22:30
问题 I would like to be able to store a matrix created in R in memory and return the pointer. And then later use the pointer to fetch back the matrix from memory. I am running R version 3.0.1 (2013-05-16) -- "Good Sport" on Ubuntu 13.01 and Rcpp version "0.10.6". I have tried ... // [[Rcpp::export]] SEXP writeMemObject(NumericMatrix mat) { XPtr<NumericMatrix> ptr(&mat, true); return ptr; } // [[Rcpp::export]] NumericMatrix getMemObject(SEXP ptr) { XPtr<NumericMatrix> out(ptr); return wrap(out); }

best way to convert DataFrame to Matrix in RCpp

二次信任 提交于 2019-12-10 02:28:49
问题 I have an RCpp code, where in a part of code I am trying to convert a DataFrame to a Matrix. The DataFrame only has numbers (no strings or dates). The following code works: //[[Rcpp::export]] NumericMatrix testDFtoNM1(DataFrame x) { int nRows=x.nrows(); NumericMatrix y(nRows,x.size()); for (int i=0; i<x.size();i++) { y(_,i)=NumericVector(x[i]); } return y; } I was wondering if there is alternative way (i.e. equivalent of as.matrix in R) in RCpp to do the same, something similar to the

Rcpp equivalent for rowsum [closed]

时光怂恿深爱的人放手 提交于 2019-12-09 23:13:51
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I am looking for a fast alternative for the R function rowsum in C++ / Rcpp / Eigen or Armadillo. The purpose is to get the sum of elements in a vector a according to a grouping vector b . For example: > a [1] 2

Why is my Rcpp implementation for finding the number of unique items slower than base R?

和自甴很熟 提交于 2019-12-09 22:27:01
问题 I'm trying to write a function to count the number of unique items in a string vector (my problem is slightly more complex, but this is reproducible. I did this based on answers I found for C++. Here is my code: C++ int unique_sort(vector<string> x) { sort(x.begin(), x.end()); return unique(x.begin(), x.end()) - x.begin(); } int unique_set(vector<string> x) { unordered_set<string> tab(x.begin(), x.end()); return tab.size(); } R: x <- paste0("x", sample(1:1e5, 1e7, replace=T)) microbenchmark

Renaming and Hiding an Exported Rcpp function in an R Package

前提是你 提交于 2019-12-09 17:37:05
问题 Writing an R package, I have a R function that calls a specific Rcpp function. The Rcpp function just serves as a helper function and I do not want to creat a .Rd file for it. My solution so far is to export both functions in the Namespace file which causes the warning to create an .Rd file for the Rcpp function as soon as I run the check command. If I remove the helper function in the Namespace file, I get rid of this warning causing now the problem that the R function is not able to find it