rcpp

integer64 and Rcpp compatibility

丶灬走出姿态 提交于 2019-12-05 19:45:14
I will need 64 bits integer in my package in a close future. I'm studying the feasibility based on the bit64 package. Basically I plan to have one or more columns in a data.table with an interger64 S3 class and I plan to pass this table to C++ functions using Rcpp. The following nanotime example from Rcpp gallery explains clearly how a vector of 64 bits int is built upon a vector of double and explain how to create an integer64 object from C++ to R. I'm now wondering how to deal with an interger64 from R to C++. I guess I can invert the principle. void useInt64(NumericVector v) { double len =

Passing mean and standard deviation into dnorm() using Rcpp Sugar

[亡魂溺海] 提交于 2019-12-05 19:12:55
I am converting some R code to Rcpp code and need to calculate the likelihood for a vector of observations given a vector of means and vector of standard deviations. If I assume the means are 0 and the standard deviations 1, I can write this function (running this requires the 'inline' and 'Rcpp' packages to be loaded), dtest1 = cxxfunction(signature( x = "numeric"), 'Rcpp::NumericVector xx(x); return::wrap(dnorm(xx, 0.0, 1.0));', plugin='Rcpp') and the result is as expected. > dtest1(1:3) [1] 0.241970725 0.053990967 0.004431848 However, if I try to make a function dtest2 = cxxfunction

Difference between value and reference args in Rcpp [duplicate]

a 夏天 提交于 2019-12-05 18:51:43
This question already has an answer here : Rcpp pass by reference vs. by value (1 answer) Closed last year . Consider these 2 functions: library(Rcpp) cppFunction("NumericVector func1(NumericVector &x) { for (int i = 0; i < x.length(); i++) x[i] = x[i] * 2; return x; }") cppFunction("NumericVector func2(NumericVector x) // no & { for (int i = 0; i < x.length(); i++) x[i] = x[i] * 2; return x; }") The only difference is that func1 takes x as a reference parameter, whereas func2 takes it as a value. If this was regular C++, I'd understand this as func1 being allowed to change the value of x in

implementing apply function in Rcpp

我怕爱的太早我们不能终老 提交于 2019-12-05 18:43:23
I have been trying to implement apply function in Rcpp so far the code looks like this //[[Rcpp::export]] NumericVector apply(NumericMatrix x,int dim,Function f){ NumericVector output; if(dim==1){ for(int i=0;i<x.nrow();i++){ output[i]=f(x(i,_)); } } else if(dim==2){ for(int i=0;i<x.ncol();i++){ output[i]=f(x(_,i)); } } return(output); } but i'm getting an error "cannot convert SEXP to double in assignment" in line 6 and 11. Is there any way to convert the value returned by an arbitrary function to double? also is there a sugar function for the apply function. There is no sugar function for

How to expose a C structure from C library to R with Rcpp

纵饮孤独 提交于 2019-12-05 16:49:42
I am trying to expose a C structure from a C library into R. For example: struct A { int flag; // ... } It is common that the library provides API to construct and destroy A : A* initA(); void freeA(A* a); Thanks for RCPP_MODULE , It is easy to expose it without considering destructor: #include <Rcpp.h> using namespace Rcpp; RCPP_EXPOSED_CLASS(A) RCPP_MODULE(A) { class_<A>("A") .field("flag", &A::flag) ; } //'@export //[[Rcpp::export]] SEXP init() { BEGIN_RCPP return wrap(*initA()); END_RCPP } I like this approach, but it might cause memory leak because it does not destruct A properly during

Rcpp and move semantic

杀马特。学长 韩版系。学妹 提交于 2019-12-05 16:49:31
I implemented an algorithm in C++ that returns as output a huge array of elements. Now, I would like to implement a wrapper in Rcpp so that I will be able to call this function by using R . I specified in the Makevars file the following setting: PKG_CXXFLAGS = -std=c++11 So that I can use the C++11 version. // [[Rcpp::export]] NumericMatrix compute(int width, int height) { vector<data_t> weights(width * height); compute_weights(weights); NumericMatrix mat(height, width); copy(begin(weights), end(weights), mat.begin()); return mat; } The above wrapper function remains efficient if the

How do I draw multinomial distributed samples with RcppArmadillo?

守給你的承諾、 提交于 2019-12-05 16:01:54
The problem is that I have a variable arma::mat prob_vec and want something equivalent to rmultinom(1, 1, prob_vec) in R. I found the rmultinom function provided by RcppArmadillo has a weird argument requirement which is different from that in R! So it won't pass the compilation. I just wanna know how to draw the desired sample in RcppArmadillo, or equivalently in Armadillo. If I need to get the pointer or convert my prob_vec variable, please tell me how. Many thanks! Dirk Eddelbuettel Your friendly neighbourhood co-author of RcppArmadillo here: I can assure you that it does not provide

Can Rcpp package DLLs be unloaded without restarting R?

前提是你 提交于 2019-12-05 12:16:31
When installing a Rcpp package on Windows, you need to ensure that the package DLL is unloaded or you get a "Permission Denied" error when copying the new DLL. This means restarting R on every recompile, which is pretty annoying. Is there any way to unload the package DLL without killing R? I've tried the detach("package:my_package", force=TRUE) command, but it doesnt unload the DLL. If you want to do this in your main R session (without using RStudio, which makes reinstalling the package and reloading R very easy), you can use devtools: library(devtools) load_all("path/to/my/package") Among

Using a different gcc version from that included with Rtools with Rcpp on Windows

不打扰是莪最后的温柔 提交于 2019-12-05 10:30:47
Before I embark on updating gcc, has anyone actually attempted this, and can they confirm building R from source is required to update the gcc version one uses to compile c++ code with Rcpp (i.e. not necessarily for package authoring and certainly not for CRAN-valid packages)? See Dirk's answer to this question, and the follow-on comment from the original poster How to use gcc 4.8.1 with Rcpp on Windows . Rebuilding R from source does not appear necessary. Here are the steps I used for a Windows 7 x64 system, running R 3.1.1 with Rtools 3.1.0.1942 . The implications of this update to gcc have

Define a matrix in R and pass it to C++

孤者浪人 提交于 2019-12-05 08:45:51
I have a matrix defined in R. I need to pass this matrix to a c++ function and do operations in C++. Example: In R, define a matrix, A <- matrix(c(9,3,1,6),2,2,byrow=T) PROTECT( A = AS_NUMERIC(A) ); double* p_A = NUMERIC_POINTER(A); I need to pass this matrix to a C++ function where variable 'data' of type vector<vector<double>> will be initialized with the matrix A. I couldn't seem to figure out how to do this. I am thinking in more complicated way then I should be, I bet there is an easy way to do this. As Paul said, I would recommend using Rcpp for that kind of things. But it also depends