rcpp

RcppEigen sparse matrix insert operation gives invalid class “dgCMatrix” error

只谈情不闲聊 提交于 2019-12-11 01:18:40
问题 I'm trying to get up to speed on using C++ to quickly build some sparse matrices for use in R. However, I cannot seem to use the insert method to change single elements of a sparse matrix in Eigen and get a correct R object of class dgCMatrix. A simple example is below. The C++ code is: #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] using Eigen::SparseMatrix; // sparse matrix // [[Rcpp::export]] SparseMatrix<double> SimpleSparseMatrix(int n) { SparseMatrix<double> new_mat(n, n); new

Rcpp jupyter notebooks

a 夏天 提交于 2019-12-11 01:14:27
问题 I have installed Rcpp in the R console with this command that until now ussually works for installing packages to run on jupyter notebooks: install.packages('Rcpp', '/home/user/anaconda3/lib/R/library/') Then in the console I do: library(Rcpp) evalCpp("2+2") and it works, but when I do the same on Jupyter notebooks it does not work, giving me this error message: /home/tyatabe/anaconda3/bin/x86_64-conda_cos6-linux-gnu-c++ -I/home/tyatabe/anaconda3/lib/R/include -DNDEBUG -I'/home/tyatabe

making 3d array with arma::cube in Rcpp shows cube error

扶醉桌前 提交于 2019-12-11 00:57:00
问题 I am making a Rcpp code for Gibbs sampling. Inside the code, I first want to make a 3 dimensional array with row number= number of iteration (500), column number=number of parameter(4) and slice number= number of chain(3). I wrote it in this way: #include <RcppArmadillo.h> #include <math.h> // [[Rcpp::depends(RcppArmadillo)]] using namespace Rcpp; using namespace std; using namespace arma; //Gibbs sampling code starts here Rcpp::List mcmc(const int iter,const int chains, const NumericVector

R cumulative sum with condition

眉间皱痕 提交于 2019-12-11 00:35:11
问题 (For those of you that are familiar with MCMC I am trying to write (a step of) the Metropolis-Hastings algorithm). I am trying to do a cumulative sum of a vector of small random values with a starting value of 0.5. However, if the cumulative sum at any point gets under 0 or over 1 I need to copy the previous value and continue on the cumulative sum, without summing the values, which would break this condition. Note: I need a vectorized solution (no loops or indices) for optimization purposes

Rcpp+Eclipse on Mac OS X

旧城冷巷雨未停 提交于 2019-12-11 00:06:11
问题 I am trying to get started using Rccp and decided to use Eclipse as a development environment since I already use StatEt for R. I am having trouble getting even a simple program to compile and run though, and would appreciate some help! Briefly I tried to follow the instructions on the blog: http://blog.fellstat.com/?p=170 exactly for setting up Rcpp, RInside and Eclipse, and for the example program. I am running on Mountain Lion, and installed g++ using the command line options in XCode. I

How can I protect a matrix in R from being altered by Rcpp?

亡梦爱人 提交于 2019-12-10 23:30:41
问题 I am making a package containing two Rcpp functions. The first function is used for creating a matrix that will be used several times by the second function. The matrix is stored in R's global environment between calls to the two functions. M <- myFirstRcpp(X) P <- mySecondRcpp(M) Depending on input parameters the second function will make changes to the input matrix (created by the first function) before calculating a vector from it ( aFunction is the C++ inside mySecondRcpp() ):

Fibonacci number in R vs Rcpp

微笑、不失礼 提交于 2019-12-10 22:34:41
问题 I was just trying to check the execution speed of Fiboncci number generation in R vs Rcpp. To my surprise, my R function was faster(also, linearly growing) than my Rcpp function. What is wrong here. The R code: fibo = function (n){ x = rep(0, n) x[1] = 1 x[2] = 2 for(i in 3:n){ x[i] = x[i-2] + x[i-1] } return(x) } The Rcpp code: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] IntegerVector fibo_sam(int n){ IntegerVector x; x.push_back(1); x.push_back(2); for(int i =2; i < n; i++){

equivalent of 'which' function in Rcpp

走远了吗. 提交于 2019-12-10 21:59:51
问题 I'm a newbie to C++ and Rcpp. Suppose, I have a vector t1<-c(1,2,NA,NA,3,4,1,NA,5) and I want to get a index of elements of t1 that are NA . I can write: NumericVector retIdxNA(NumericVector x) { // Step 1: get the positions of NA in the vector LogicalVector y=is_na(x); // Step 2: count the number of NA int Cnt=0; for (int i=0;i<x.size();i++) { if (y[i]) { Cnt++; } } // Step 3: create an output matrix whose size is same as that of NA // and return the answer NumericVector retIdx(Cnt); int

Read C++ binary file in R

别等时光非礼了梦想. 提交于 2019-12-10 20:51:45
问题 Can I read a binary file written by C++ in R? I have been using Rcpp in my R package and the simulations typically generate a large amount of data. I am planning to write the output to binary files in C++ and then read those back in R. This works if I write as text files but I didn't find a solution with binary files. The program sometimes crashes abruptly if I pass data using many NumericVectors (I am yet to fully understand the memory management using Rcpp). Can this approach enable me to

How can I add a new column to dataframe in RCpp?

耗尽温柔 提交于 2019-12-10 19:48:48
问题 I am trying to add a new column to data frame using RCpp. In the following codes, I intend to add a "result" column to dataframe, df. But the dataset does not have "result" column after running the codes. Could you tell me what is wrong with them? R file to call AddNewCol() function. library(Rcpp) sourceCpp('AddNewCol.cpp') AddNewCol( df ,"result") AddNewCol.cpp #include <Rcpp.h> #include<math.h> using namespace Rcpp; // [[Rcpp::export]] void AddNewCol(DataFrame& df, std::string new_var) {