rcpp

Rcpp: my distance matrix program is slower than the function in package

醉酒当歌 提交于 2019-11-28 09:47:18
问题 I would like to calculate the pairwise euclidean distance matrix. I wrote Rcpp programs by the suggestion of Dirk Eddelbuettel as follows NumericMatrix calcPWD1 (NumericMatrix x){ int outrows = x.nrow(); double d; NumericMatrix out(outrows,outrows); for (int i = 0 ; i < outrows - 1; i++){ for (int j = i + 1 ; j < outrows ; j ++){ NumericVector v1= x.row(i); NumericVector v2= x.row(j); NumericVector v3=v1-v2; d = sqrt(sum(pow(v3,2))); out(j,i)=d; out(i,j)=d; } } return (out) ; } But I find my

efficiently locf by groups in a single R data.table

谁说胖子不能爱 提交于 2019-11-28 09:16:01
I have a large, wide data.table (20m rows) keyed by a person ID but with lots of columns (~150) that have lots of null values. Each column is a recorded state / attribute that I wish to carry forward for each person. Each person may have anywhere from 10 to 10,000 observations and there are about 500,000 people in the set. Values from one person can not 'bleed' into the following person, so my solution must respect the person ID column and group appropriately. For demonstration purposes - here's a very small sample input: DT = data.table( id=c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), aa=c("A", NA,

R: C++ Optimization flag when using the inline package

杀马特。学长 韩版系。学妹 提交于 2019-11-28 09:03:51
In R when using the cxxfunction from the inline package , how does one change the optimization flag for the cpp compiler? By default, on my machine, it compiles with -g -O2 . But I would like to use the -O3 optimization to gain speed. I use the Rcpp plugin if that makes any difference. I have tried creating my own plugin, and I have tried to set the different arguments of the cxxfunction but nothing worked. I guess one option would be to compile it using R CMD SHLIB instead of using cxxfunction . But Rcpp recommends the use of inline because most of their test cases are using it. thanks for

how can I handle vectors without knowing the type in Rcpp

假如想象 提交于 2019-11-28 07:41:11
I want to replicate the following R function in Rcpp : fR = function(x) x[1:2] fR(c(1,2,3)) #[1] 1 2 fR(c('a','b','c')) #[1] "a" "b" I can do it for a fixed output type like so: library(inline) library(Rcpp) fint = cxxfunction(signature(x = "SEXP"), ' List xin(x); IntegerVector xout; for (int i = 0; i < 2; ++i) xout.push_back(xin[i]); return xout;', plugin = "Rcpp") But this will only work for integers, and if I try replacing the xout type with List (or GenericVector , which are the same) - it works with any input type, but I get back a list instead of a vector. What's the correct Rcpp way of

Debugging (line by line) of Rcpp-generated DLL under Windows

Deadly 提交于 2019-11-28 07:31:44
Recently I've been experimenting with Rcpp (inline) to generate DLLs that perform various tasks on supplied R inputs. I'd like to be able to debug the code in these DLLs line by line, given a specific set of R inputs. (I'm working under Windows.) To illustrate, let's consider a specific example that anybody should be able to run... The code below is a really simple cxxfunction which simply doubles the input vector. Note however that there's an additional variable myvar that changes value a few times but doesn't affect the output - this has been added so that we'll be able to see when the

Error using Rcpp::Nullable with RcppArmadillo types

自古美人都是妖i 提交于 2019-11-28 06:24:14
问题 I'm using RcppArmadillo in my R package and I would like to use the Rcpp::Nullable in the parameter list. NumericVector d_snb(NumericVector& x, Nullable<arma::mat> size_param = R_NilValue, const int& infinite = 100000, const bool& log_v = false) This gives an error like: Error: package or namespace load failed for ‘packagex’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so': dlopen(/usr/local/lib/R/3.4/site

How to get a big sparse matrix in R? (> 2^31-1)

◇◆丶佛笑我妖孽 提交于 2019-11-28 05:01:07
问题 I use some C++ code to take a text file from a database and create a dgcMatrix type sparse matrix from the Matrix package. For the first time, I'm trying to build a matrix that has more than 2^31-1 non-sparse members, which means that the index vector in the sparse matrix object must also be longer than that limit. Unfortunately, vectors seem to use 32-bit integer indices, as do NumericVectors in Rcpp. Short of writing an entire new data type from the ground up, does R provide any facility

How do I create a list of vectors in Rcpp?

别说谁变了你拦得住时间么 提交于 2019-11-28 04:31:40
I'm writing an Rcpp module an would like to return as one element of the RcppResultSet list a list whose elements are vectors. E.g., .Call("myfunc")$foo should be something like: [[1]] [1] 1 [[2]] [1] 1 1 [[3]] [1] 1 1 1 (the exact numbers are not important here). The issue is that I don't know the right Rcpp way of doing this. I tried passing a vector<vector<int> > but this constructs a matrix by silently taking the length of the first vector as the width (even if the matrix is ragged!). I've tried constructing an RcppList but have a hard time casting various objects (like RcppVector ) safely

Getting user input from R console: Rcpp and std::cin

痞子三分冷 提交于 2019-11-28 04:22:06
问题 I have been doing some exercises to learn c++ and decided to integrate them into R since ultimately I want to write c++ backends for R functions. I am having trouble finding a solution to retrieve user input from the R console. While there is Rcpp::Rcout for printing and returning output, there doesn't seem to be a similar funciton for std::cin.... #include <Rcpp.h> // [[Rcpp::export]] Rcpp::String cola() { Rcpp::Rcout << "Pick a drink:" << std::endl << "1 - Espresso" << std::endl << "2 -

Understanding the contents of the Makevars file in R (macros, variables, ~/.R/Makevars and pkg/src/Makevars)

筅森魡賤 提交于 2019-11-28 02:48:25
I am trying to understand the role and relationship of the macros/variables set in ~/.R/Makevars and package_directory/src/Makevars when installing/building own R packages. Suppose these files look like ~/.R/Makevars CXX = g++ CXXSTD = -std=c++11 CXXFLAGS = -fsanitize=undefined,address -fno-omit-frame-pointer CXX98 = g++ CXX98STD = -std=c++98 CXX11 = g++ CXX11STD = -std=c++11 CXX14 = g++ CXX14STD = -std=c++14 package_directory/src/Makevars PKG_CPPFLAGS = -I../inst/include CXX_STD = CXX11 As I understand it, with CXX we can select the compiler for C++ when building R packages, with CXXSTD we