rcpp

Transform a boost::array into NumericVector in Rcpp

喜欢而已 提交于 2019-12-02 06:09:50
In my C++ script (run in R using Rcpp), I defined : typedef boost::array< double ,3 > state_type; Now, I want to create a function to transform a state_type variable to a Rcpp::NumericVector variable, and another function that do the inverse. How can do that? I need to do that in order to use R function into C++. How about Rcpp::NumericVector boost_array_to_nvec(state_type const& s) { Rcpp::NumericVector nvec(s.size()); for (size_t i = 0; i < s.size(); ++i) { nvec[i] = s[i]; } return nvec; } state_type nvec_to_boost_array(Rcpp::NumericVector const& nvec) { state_type s; for (size_t i = 0; i <

Is there a limit on working with matrix in R with Rcpp?

非 Y 不嫁゛ 提交于 2019-12-02 03:44:53
I was trying to develop a program in R to estimate a Spearman correlation with Rcpp. I did it, but it only works with matrix with less of a range between 45 00 - 50 000 vectors. I don't know why, but it only works with that dimension. I suppose there's limit with that type of information, maybe if I work it like a data.frame? I would really appreciate if someone gives me insight. Here i post my code. Ive been trying to limit the max integer number that i call "denominador", which exceeds it. Maybe you could help me. cppFunction('double spearman(NumericMatrix x){ int nrow = x.nrow(), ncol = x

Why does `fastLm()` return results when I run a regression with one observation?

荒凉一梦 提交于 2019-12-02 03:30:01
Why does fastLm() return results when I run regressions with one observation? In the following, why aren't the lm() and fastLm() results equal? library(Rcpp) library(RcppArmadillo) library(data.table) set.seed(1) DT <- data.table(y = rnorm(5), x1 = rnorm(5), x2 = rnorm(5), my.key = 1:5) # y x1 x2 my.key # 1: -0.6264538 -0.8204684 1.5117812 1 # 2: 0.1836433 0.4874291 0.3898432 2 # 3: -0.8356286 0.7383247 -0.6212406 3 # 4: 1.5952808 0.5757814 -2.2146999 4 # 5: 0.3295078 -0.3053884 1.1249309 5 lm(y ~ 1 + x1 + x2, data = DT[my.key == 1]) # Coefficients: # (Intercept) x1 x2 # -0.6265 NA NA fastLm(X

Rcpp: Syntactic sugar for * produces unexpected results when dealing with NumericMatrix

我的梦境 提交于 2019-12-02 00:25:45
问题 A recently asked question has lead me to believe the syntactic sugar for * by Rcpp does not work as intended. In the linked question, the user is trying to multiply a matrix by a scalar. R code Here's what we're trying to achieve in Rcpp , but for now in plain R : > m <- matrix(0:3, 2, 2) > m * 3 [,1] [,2] [1,] 0 6 [2,] 3 9 Rcpp Code I've created some minimal examples demonstrating both the problem above, and also some unexpected behaviour along the way. First note that I'm consistently using

Rcpp: Syntactic sugar for * produces unexpected results when dealing with NumericMatrix

落花浮王杯 提交于 2019-12-02 00:13:40
A recently asked question has lead me to believe the syntactic sugar for * by Rcpp does not work as intended. In the linked question, the user is trying to multiply a matrix by a scalar. R code Here's what we're trying to achieve in Rcpp , but for now in plain R : > m <- matrix(0:3, 2, 2) > m * 3 [,1] [,2] [1,] 0 6 [2,] 3 9 Rcpp Code I've created some minimal examples demonstrating both the problem above, and also some unexpected behaviour along the way. First note that I'm consistently using List as a return type, because it removes the need for me to declare the appropriate type in advance:

*.o: File format not recognized on Windows 7

半世苍凉 提交于 2019-12-02 00:09:41
I wrote an R package called arbintools for some work-related data analysis and put it on Github . I wrote it and have been using it on my Mac for a while without issue; Today, I tried to install the dev-1 branch on a Windows 7 laptop and something related to compiling some Rcpp functions seems to go wrong: devtools::install_github("mjlacey/arbintools", ref = "dev1") I get this: Downloading GitHub repo mjlacey/arbintools@dev1 from URL https://api.github.com/repos/mjlacey/arbintools/zipball/dev1 Installing arbintools "C:/PROGRA~1/R/R-33~1.0/bin/x64/R" --no-site-file --no-environ --no-save \ --no

'unlockEnvironment' implemented via 'Rcpp' instead of 'inline'

穿精又带淫゛_ 提交于 2019-12-01 23:48:32
问题 Actual question Could someone get me started on what I need to do to implement the code of unlockEnvironment below in Rcpp? Background Came across this post and tried Winston Chang's solution based on C code with inline. It works, but I have the feeling I know too little (practically nothing, that is) about either inline or C/C++ to really know what I'm doing ;-) So I thought this would be a great opportunity to finally start learning on how to use R as an interface to C and C++. And I think

RStudio crashes with RCpp with reproducible codes

送分小仙女□ 提交于 2019-12-01 21:26:44
@user3759195 wrote a post https://stackoverflow.com/questions/24322356/rstudio-crashes-and-it-does-not-reproduce about RStudio crashing with RCpp, but didn't give any reproducible case. @KevinUshey mentioned in the comments that we have to PROTECT the wrap within the code. I took the liberty of posting two alternatives to split.data.frame function written in RCpp: * VERSION THAT DOES NOT CRASH RSTUDIO * //[[Rcpp::export]] List splitDataFrameCpp(DataFrame x,NumericVector y) { int nRows=x.nrows(); int nCols=x.size(); std::map<double,vector<double> > z; for (int i=0;i<nCols;i++) { std::vector

How create an Rcpp NumericVector with more than 20 entries?

梦想与她 提交于 2019-12-01 19:04:23
Creating a NumericVector with more than 20 elements leads to error messages. This is in agreement with this document (at the very bottom): http://statr.me/rcpp-note/api/Vector_funs.html Currently, I expose a class (using RCPP_MODULE) of which one of its methods returns the desired NumericVector. How can I return more than 20 elements? #include <Rcpp.h> class nvt { public: nvt(int x, double y) {...} NumericVector run(void) { .... return NumericVector::create(_["a"]=1,_["b"]=2, .....,_["c"]=21); } }; RCPP_MODULE(nvt_module){ class_<nvt>("nvt") .constructor<int,double>("some description") .method

Rcpp function to be SLOWER than same R function

谁说我不能喝 提交于 2019-12-01 18:44:57
问题 I have been coding a R function to compute an integral with respect to certain distributions, see code below. EVofPsi = function(psi, probabilityMeasure, eps=0.01, ...){ distFun = function(u){ probabilityMeasure(u, ...) } xx = yy = seq(0,1,length=1/eps+1) summand=0 for(i in 1:(length(xx)-1)){ for(j in 1:(length(yy)-1)){ signPlus = distFun(c(xx[i+1],yy[j+1]))+distFun(c(xx[i],yy[j])) signMinus = distFun(c(xx[i+1],yy[j]))+distFun(c(xx[i],yy[j+1])) summand = c(summand, psi(c(xx[i],yy[j]))*