rcpp

Sample a random integer in Rcpp

只愿长相守 提交于 2019-12-09 16:35:58
问题 This might be a very easy question, but I can't really find it. How can I sample a random integer between 1 and N in Rcpp? I know I can sample a uniform number and round it, or include the sample function to use in the C++ code, but both don't seem like the correct way of doing this. 回答1: AFIACR we do not have a "decent" (ie vectorised) sample() function in Rcpp-land, but Christian Gunning is working on one based on Armadillo via RcppArmadillo. You can of course sample from U(0,1), scale to N

How to return R's NULL in Rcpp code?

随声附和 提交于 2019-12-09 15:52:26
问题 Suppose I have a C++ code to compile with Rcpp and will be called in R. // [[Rcpp::export]] SEXP to_env(List x) { if(x.hasAttribute("names")) { return x; } else { return NULL; } } What should the NULL value be to return R's NULL instead of a crash? 回答1: Use this code: return R_NilValue; 来源: https://stackoverflow.com/questions/25658225/how-to-return-rs-null-in-rcpp-code

Errors installing plyr / rcpp

China☆狼群 提交于 2019-12-09 11:32:48
问题 I have two computers and in one of them I can't manage to install the plyr package for R. This is the error I get: * installing *source* package ‘plyr’ ... ** package ‘plyr’ successfully unpacked and MD5 sums checked ** libs g++ -I/usr/share/R/include -DNDEBUG -I"/usr/lib/R/site-library/Rcpp/include" -fpic -O2 -pipe -g -c RcppExports.cpp -o RcppExports.o RcppExports.cpp: En la función ‘SEXPREC* plyr_loop_apply(SEXP, SEXP)’: RcppExports.cpp:15:9: error: ‘input_parameter’ no es un miembro de

Most efficient/vectorization when using previous calculated value (rolling)

ぃ、小莉子 提交于 2019-12-09 06:12:38
问题 Following those conversations: Can I vectorize a calculation which depends on previous elements sapply? tapply? ddply? dataframe variable based on rolling index of previous values of another variable I wanted to test a more "real-life" case-study. I recently had to migrate SAS code to R and kdb code to R code. I tried to compiled a simple enough yet more sophisticated example to optimize. let's build the training set buildDF <- function(N){ set.seed(123); dateTimes <- sort(as.POSIXct("2001-01

Overriding system defaults for C++ compilation flags from R

房东的猫 提交于 2019-12-08 17:07:46
问题 I'm using RcppEigen to write some C++ functions for my R code, and I'd like to optimize their compilation as much as possible. When I've used Eigen in the past, I've gotten a significant boost from -O3 and -fopenmp. Following Dirk's advice, I edited ~/.R/Makevars so that my Eigen code would be compiled with these flags: CPPFLAGS=-O3 -fopenmp This works--when I check what's happening during compilation (ps ax | grep cpp) I see: 27097 pts/6 R+ 0:00 /usr/libexec/gcc/x86_64-redhat-linux/4.4.7

Exceeded maximum number of DLLs in R

喜夏-厌秋 提交于 2019-12-08 15:28:40
问题 I am using RStan to sample from a large number of Gaussian Processes (GPs), i.e., using the function stan(). For every GP that I fit, another DLL gets loaded, as can be seen by running the R command getLoadedDLLs() The problem I'm running into is that, because I need to fit so many unique GPs, I'm exceeding the maximum number of DLLs that can be loaded, at which point I receive the following error: Error in dyn.load(libLFile) : unable to load shared object '/var/folders/8x

Compiling multiple source files in Rcpp

≯℡__Kan透↙ 提交于 2019-12-08 14:27:29
I have the following directory structure my_func - my_func_r.cpp - my_func.c - my_func.h - my_func_test.c - matrix/ - matrix.h - matrix.c The matrix directory contains some matrix structures in matrix.h and some initialisation, free, print etc. functions in matrix.c . The my_func.h file is something like #include <math.h> #include <stdio.h> #include <stdlib.h> #include "matrix/matrix.h" ... some structures and templates ... The my_func.c file is then #include "my_func.h" ... helper functions ... int my_func(...) { ... my_func stuff ... return 0; } The my_func_test.c is something like #include

Ordering dataframe using Rcpp & Dplyr

落花浮王杯 提交于 2019-12-08 14:20:17
问题 first post on this forum so feel free to go easy :-) I am trying to sort a dataframe using Rcpp/dplyr. Based on an earlier post from Romain - the idea is to use the OrderVisitor class as explained in this post. order a dataframe by column in Rcpp My problem is I cannot compile the MyFunc code below as defined in above post. C++: enter code here #include <Rcpp.h> #include <dplyr.h> using namespace Rcpp; using namespace dplyr; // [[Rcpp::export]] // [[Rcpp::depends(dplyr)]] DataFrame myFunc

How to find the index of the minimum value between two specific columns of a matrix with Rcpp?

南笙酒味 提交于 2019-12-08 14:19:55
问题 I have a 5x5 matrix and want to find the index of the smallest value between columns "1" and "3". In R I would do like this: set.seed(1984) m <- matrix(sample.int(25,25), 5) min <- which(m[,c(1,3)] == min(m[,c(1,3)]), arr.ind = TRUE) What is the most efficient way of doing that with Rcpp? 回答1: I would opt to use RcppArmadillo over Rcpp as it has more robust matrix manipulation. For instance, you can find the index_min() or index_max() of a subset quickly and then translate it to subscript

RInside installation error

為{幸葍}努か 提交于 2019-12-08 12:56:36
问题 I am trying to make a R development environment in my computer. During last hour, I could successfully install RTools and RCpp. But when I tried the last element RInside package, I get following error. I can see the reason is because g++ or make does not recognize path including space (Program Files -> Files) But how can I change the make file or batter question is ... where is the make file producing following compilation? > install.packages(c("RInside"),type="source") trying URL 'http:/