rcpp

Extracting a column with NA's from a bigmemory object in Rcpp

徘徊边缘 提交于 2019-12-08 12:15:12
问题 I'm trying to create a function that extracts a column from a big.matrix object in Rcpp (so that it can be analyzed in cpp before bringing the results to R), but I can't figure out how to get it to recognise NA's (they are now presented as -2147483648 - as shown in my minimal example below). It would be even better if I could access the function GetMatrixCols ( src/bigmemory.cpp ) straight from Rcpp, but I've yet to discover a way to do that. #include <Rcpp.h> // [[Rcpp::plugins(cpp11)]] // [

issue with cxxfunction of package inline

无人久伴 提交于 2019-12-08 10:43:38
问题 I was trying to run the colMaxRCpp function provided by Dirk Eddelbuettel in this post. I'm just repeating the function here so that folks reading this post don't have to click on the link. library(inline) colMaxRcpp <- cxxfunction(signature(X_="numeric"), plugin="Rcpp", body=' Rcpp::NumericMatrix X(X_); int n = X.ncol(); Rcpp::NumericVector V(n); for (int i=0; i<n; i++) { Rcpp::NumericVector W = X.column(i); V[i] = *std::max_element(W.begin(), W.end()); // from the STL } return(V); ') When I

Compiling RInside program with g++ on Linux

删除回忆录丶 提交于 2019-12-08 09:55:01
问题 I have installed R through RPM (with Yast). anisha@linux-y3pi:~> locate RInside.so /usr/lib64/R/library/RInside/lib/libRInside.so /usr/lib64/R/library/RInside/libs/RInside.so anisha@linux-y3pi:~> locate Rcpp.so /usr/lib64/R/library/Rcpp/lib/libRcpp.so /usr/lib64/R/library/Rcpp/libs/Rcpp.so The file ( rinsidetest.cpp ) that I wish to compile contains: #include <RInside.h> #include <Rcpp.h> int main (int argc, char *argv[]) { RInside R (argc, argv); return 0; } Here's how I compile it: anisha

Rcpp omp_set_num_threads within a package

℡╲_俬逩灬. 提交于 2019-12-08 08:49:00
问题 I wrote the following simple example with Rcpp and OpenMP that works fine when I source the cpp file from RStudio: #include <Rcpp.h> #include <omp.h> // [[Rcpp::plugins(openmp)]] using namespace Rcpp; // [[Rcpp::export]] NumericMatrix my_matrix(int I, int J, int nthreads) { NumericMatrix A(I,J); int i,j,tid; omp_set_num_threads(nthreads); #pragma omp parallel for private(i, j, tid) for(int i = 0; i < I; i++) { for(int j = 0; j < J; j++) { tid = omp_get_thread_num(); A(i,j) = tid ; } } return

Eigen's LeastSquaresConjugateGradient solver: using Incomplete Cholesky preconditioner and specifying coefficient starting values

↘锁芯ラ 提交于 2019-12-08 07:26:23
问题 To solve a rectangular sparse linear system of equations I would like to use Eigen's LeastSquaresConjugateGradient. Aside from the default Jacobi and Identity preconditioner I was wondering though if it is possible to also use Incomplete Cholesky as a preconditioner within LeastSquaresConjugateGradient? The Rcpp code I have and which uses the default Jacobi ([LeastSquareDiagonalPreconditioner) preconditioner is: library(inline) library(RcppEigen) solve_sparse_lsconjgrad <- cxxfunction(

Large SpMat object with RcppArmadillo

浪子不回头ぞ 提交于 2019-12-08 07:24:24
问题 I am trying to learn and use Rcpp and RcppArmadillo for the sparse linear algebra routines. Code below is adaptation of the example here: http://gallery.rcpp.org/articles/armadillo-sparse-matrix/ code <- ' S4 matx(x); IntegerVector Xd = matx.slot("Dim"); IntegerVector Xi = matx.slot("i"); IntegerVector Xp = matx.slot("p"); NumericVector Xx = matx.slot("x"); arma::sp_mat Xsp(Xd[0], Xd[1]); // create space for values, and copy arma::access::rw(Xsp.values) = arma::memory::acquire_chunked<double>

How to calculate integral, numerically, in Rcpp

前提是你 提交于 2019-12-08 05:30:13
问题 I've searched for an hour for the methods doing numerical integration. I'm new to Rcpp and rewriting my old programs now. What I have done in R was: x=smpl.x(n,theta.true) joint=function(theta){# the joint dist for #all random variable d=c() for(i in 1:n){ d[i]=den(x[i],theta) } return(prod(d)*dbeta(theta,a,b)) } joint.vec=Vectorize(joint)##vectorize the function, as required when ##using integrate() margin=integrate(joint.vec,0,1)$value # the ##normalizeing constant at the donominator area

fastLm() is much slower than lm()

余生长醉 提交于 2019-12-08 05:06:25
问题 fastLm() is much slower than lm() . Basically, I just call lm() and fastLm() with the same formula and data, but fastLm() seems to be much slower than lm() . Is this possible? I just don't know how could this happen? dim(dat) #[1] 87462 90 ## library(Rcpp) library(RcppEigen) library(rbenchmark) benchmark(fastLm(formula(mez),data=dat),lm(formula(mez),data=dat)) test replications elapsed relative user.self sys.self user.child sys.child 1 fastLm(formula(mez), data = dat) 100 195.81 7.079 189.36

Calling R function from Rcpp

空扰寡人 提交于 2019-12-08 05:04:28
问题 I have a very basic question about C++ integration in R via Rcpp. Suppose I want to implement a simple function like this one in C++: inte = function(x, y, a, b){ model = approxfun(x, y) return(integrate(model, a, b)$value) } So a very basic approach would be to call R's function 'integrate' as much as needed: // [[Rcpp::export]] double intecxx(Function inte, NumericVector x, NumericVector y, double a, double b) { NumericVector res; res = inte(x, y, a, b); return res[0]; } However, I need to

Saving Lattice Plots with RInside and Rcpp

♀尐吖头ヾ 提交于 2019-12-08 04:58:51
问题 I am trying to build an R application in C++ using RInside. I wanted to save the plots as images in specified directory using codes, png(filename = "filename", width = 600, height = 400) xyplot(data ~ year | segment, data = dataset, layout = c(1,3), type = c("l", "p"), ylab = "Y Label", xlab = "X Label", main = "Title of the Plot") dev.off() It creates a png file in the specified directory if directly run from R. But using C++ calls from RInside, I was not able to reproduce the same result. (