rcpp

NA values in Rcpp conditional

北慕城南 提交于 2019-12-04 02:36:28
I am having trouble with conditionals in Rcpp. The best way to explain my problem is through an example. z <- seq(from=1,to=10,by=0.1) z[c(5,10,15,20,40,50,80)] <- NA src <- ' Rcpp::NumericVector vecz(z); for (int i=0;i<vecz.size();i++) { if (vecz[i] == NA_REAL) { std::cout << "Here is a missing value" << std::endl; } } ' func <- cxxfunction(signature(z="numeric"),src,plugin="Rcpp") func(z) # NULL From my understanding, NA_REAL represents the NA values for the reals in Rcpp and NA_Integer represents the NA values for integers. I'm not sure why the above conditional never returns true given z .

How to return R's NULL in Rcpp code?

不羁岁月 提交于 2019-12-04 02:12:09
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? Use this code: return R_NilValue; 来源: https://stackoverflow.com/questions/25658225/how-to-return-rs-null-in-rcpp-code

Error installing and running rcpp

浪子不回头ぞ 提交于 2019-12-04 01:01:35
问题 I'm pretty new to R so apologies for a stupid question. I'm trying to get rcpp running but I'm stuck in an endless loop of R asking me to re-install RTools. I broadly followed the code in this blog post, although first time off I installed everything by hand & I've subsequently re-installed everything a few times over. I'm running Windows 7, R version 3.1.2, R Studio Version 0.98.1091 (not that this should matter much) and RTools 3.1. An edited highlight of what my console looks like is as

How to set g++ compiler flags using Rcpp and inline?

有些话、适合烂在心里 提交于 2019-12-04 00:31:42
问题 I want to set -std=c++0x , using Rcpp with inline. I saw R: C++ Optimization flag when using the inline package but don't want to make a system-wide change, so I was trying option 2 in Dirk's answer. I tried: settings=getPlugin("Rcpp") settings$Makevars[length(settings$Makevars)+1] = "CXXFLAGS = $(CXXFLAGS) -std=c++0x" fun=cxxfunction(signature(x_ ="numeric"),src,plugin="Rcpp",settings=settings,verbose=2); But the verbose output shows it is ignoring that. I also tried with CFLAGS, and without

R - use primitive functions like max(), sum() in Rcpp

房东的猫 提交于 2019-12-03 17:09:52
The following codes: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector FFF(){ NumericVector LB(3); LB[0]=Language("max",12.3,1.2,13.3,34,10,12.45).eval(); LB[1]=Language("min",12.31,1.24,13.35,340,109,121.45).eval(); LB[2]=Language("sum",12.37,1.21,13.43,34).eval(); return LB; } won't pass the compiler, since "Language("max",12.3,1.2,13.3,34,10,12.45).eval())" returns SEXP object, which doesn't fit LB[0]'s type "double". I really want to directly use max(), min() and sum() from the R base instead of writing additional C++ functions. Do you have any good idea? Thank you!

Applying the optim function in R in C++ with Rcpp

本秂侑毒 提交于 2019-12-03 17:08:44
I am trying to call R function optim() in Rcpp . I saw an example in Calling R's optim function from within C++ using Rcpp , but I am unable to modify it correctly for my use case. Basically, the objective function depends on the x and y but I want to optimize it with respect to b . Here is the R code that does what I want: example_r = function(b, x, y) { phi = rnorm(length(x)) tar_val = (x ^ 2 + y ^ 2) * b * phi objftn_r = function(beta, x, y) { obj_val = (x ^ 2 + y ^ 2) * beta return(obj_val) } b1 = optim(b, function(beta) { sum((objftn_r(beta, x, y) - tar_val) ^ 2) }, method = "BFGS")$par

Can Rcpp replace unif function in R?

我只是一个虾纸丫 提交于 2019-12-03 15:18:44
I have just started using the Rcpp package in R, my learning is inspired by the Advanced R course by Hadley Wickham. Within R studio I have the following .cpp file. The question is more general but this example helps. #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector runifC(int n, double min=0, double max=1) { NumericVector out(n); for(int i = 0; i < n; ++i) { out[i] = min + ((double) rand() / (RAND_MAX)) * (max - min); } return out; } /*** R library(microbenchmark) microbenchmark( 'R unif-1' = runif(1), 'C++ unif-1' = runifC(1), 'R unif-100' = runif(100), 'C++ unif-100

Why is standard R median function so much slower than a simple C++ alternative?

戏子无情 提交于 2019-12-03 15:03:47
问题 I made the following implementation of the median in C++ and and used it in R via Rcpp : // [[Rcpp::export]] double median2(std::vector<double> x){ double median; size_t size = x.size(); sort(x.begin(), x.end()); if (size % 2 == 0){ median = (x[size / 2 - 1] + x[size / 2]) / 2.0; } else { median = x[size / 2]; } return median; } If I subsequently compare the performance with the standard built-in R median function, I get the following results via microbenchmark > x = rnorm(100) >

Errors installing plyr / rcpp

半城伤御伤魂 提交于 2019-12-03 13:56:01
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 ‘Rcpp::traits’ RcppExports.cpp:15:40: error: expected primary-expression before ‘int’ RcppExports.cpp:15

Using Rcpp with Windows-specific includes

怎甘沉沦 提交于 2019-12-03 12:59:13
I'm trying to write some C++ code that accesses some OS-level things in Windows, using Rcpp. As soon as I include windows.h or shlobj.h , I get a bunch of compilation errors. When I run this code, it works, so I know I'm getting some of the basics right. But when I uncomment either of the Windows-related #include lines, it doesn't work. library(inline) inc <- ' #include <iostream> #include <stdio.h> // #include <windows.h> // #include <shlobj.h> using namespace std; ' src <- ' cout << "foo\\n"; printf("foo2\\n"); return Rcpp::wrap(20); ' fun <- cxxfunction(signature(), includes = inc, src,