rcpp

Error installing and running rcpp

时光毁灭记忆、已成空白 提交于 2019-12-01 03:43:26
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 follows: > library(installr) Welcome to installr version 0.15.3... > install.Rtools() Loading required

Default NULL parameter Rcpp

家住魔仙堡 提交于 2019-12-01 03:38:15
I am trying to define a function with a default NULL parameter in Rcpp . Following is an example: // [[Rcpp::export]] int test(int a, IntegerVector kfolds = R_NilValue) { if (Rf_isNull(kfolds)) { cout << "NULL" << endl; } else { cout << "NOT NULL" << endl; } return a; } But when I run the code: test(1) I get the following error: Error: not compatible with requested type How can I solve this issue? You are in luck. We needed this in mvabund and Rblpapi , and have it since the last (two) Rcpp releases. So try this: // [[Rcpp::export]] int test(int a, Rcpp::Nullable<Rcpp::IntegerVector> kfolds =

Default NULL parameter Rcpp

别说谁变了你拦得住时间么 提交于 2019-11-30 23:41:51
问题 I am trying to define a function with a default NULL parameter in Rcpp . Following is an example: // [[Rcpp::export]] int test(int a, IntegerVector kfolds = R_NilValue) { if (Rf_isNull(kfolds)) { cout << "NULL" << endl; } else { cout << "NOT NULL" << endl; } return a; } But when I run the code: test(1) I get the following error: Error: not compatible with requested type How can I solve this issue? 回答1: You are in luck. We needed this in mvabund and Rblpapi, and have it since the last (two)

Rcpparmadillo: can't call Fortran routine “dgebal”?

荒凉一梦 提交于 2019-11-30 23:32:30
I need to use a Fortran routine called dgebal (documentation here ) in my Rcpparmadillo code. I have included the following headers: # include <RcppArmadillo.h> # include <math.h> However, when I try to compile my code using sourceCpp() I get the following error: error: 'dgebal_' was not declared in this scope If I further include <R_ext/Lapack.h> and <R_ext/BLAS.h> , the code compiles without error and runs fine. However the compiler throws a bunch of warnings like this: C:/PROGRA~1/R/R-32~1.3/include/R_ext/BLAS.h:49:64: warning: declaration of 'double dasum_(const int*, const double*, const

using rinside with qt in windows

天大地大妈咪最大 提交于 2019-11-30 22:12:13
I am beginning using rinside and rcpp within c++. I just want to start from zero so my QT project has nothing but the creation of a RInside instance and I have a problem I cannot solve. I have only one dialog form in the project. My project file: QT += core gui TARGET = rcpp-rinside TEMPLATE = app SOURCES += main.cpp\ dialog.cpp HEADERS += dialog.h FORMS += dialog.ui INCLUDEPATH += C:\R\R-2.15.1\include INCLUDEPATH += C:\R\R-2.15.1\library\Rcpp\include INCLUDEPATH += C:\R\R-2.15.1\library\RInside\include LIBS += -LC:\R\R-2.15.1\bin\i386 -lR LIBS += -LC:\R\R-2.15.1\library\Rcpp\lib\i386\ -lRcpp

Slice a string at consecutive indices with R / Rcpp?

随声附和 提交于 2019-11-30 21:31:29
I want to write a function that slices a 'string' into a vector, sequentially, at a given index. I have a fairly adequate R solution for it; however, I figure that writing the code in C/C++ would likely be faster. For example, I'd like to be able to write a function 'strslice' that operates as follows: x <- "abcdef" strslice( x, 2 ) ## should return c("ab", "cd", "ef") However, I'm not sure how to handle treating elements of the 'CharacterVector' passed around in the Rcpp code as strings. This is what I imagine might work (given my lack of C++/Rcpp knowledge I'm sure there's a better approach)

Why does assert not work here?

假如想象 提交于 2019-11-30 21:29:35
Here is the code: #include <Rcpp.h> #include <iostream> #include <assert.h> #include <stdio.h> using namespace Rcpp; // [[Rcpp::export]] double eudist(NumericVector x, NumericVector y) { int nx = x.size(); int ny = y.size(); std::cout << nx << '\n' << ny << std::endl; assert(nx == ny); double dist=0; for(int i = 0; i < nx; i++) { dist += pow(x[i] - y[i], 2); } return sqrt(dist); } After sourcing it into R, I get the following result, apparently it does not abort when there is an error: #//////////////////////////////////////////////////// sourceCpp('x.cpp') #///////////////////////////////////

Rcpp NumericMatrix - how to erase a row / column?

自作多情 提交于 2019-11-30 19:48:13
A novice question as I learn the Rcpp classes / data structures: Is there a member function to erase a row / column for an object of class Rcpp::NumericMatrix ? (Or other types of type **Matrix -- I'm assuming it's a template class)? library(Rcpp) cppFunction(' NumericMatrix sub1 {NumericMatrix x, int& rowID, int& colID) { // let's assume separate functions for rowID or colID // but for the example case here x.row(rowID).erase(); // ??? does this type of member function exist? x.col(colID).erase(); // ??? return x; }') If this type of member function doesn't exist, how about this? cppFunction(

How should I count the number of unique rows in a 'binary' matrix?

十年热恋 提交于 2019-11-30 18:39:53
Suppose I have a matrix whose entries are only 0 and 1 , e.g. set.seed(123) m <- matrix( sample(0:1, 10, TRUE), nrow=5 ) with sample output: [,1] [,2] [1,] 0 0 [2,] 1 1 [3,] 0 1 [4,] 1 1 [5,] 1 0 The matrix will have at most 20 columns, and will have many rows. I want a function, let's call it rowCounts , that returns: The number of times a particular row appears in the matrix, and The index of the first occurrence of that row. How might I solve this problem? Building on Kevin's answer, here is a C++11 version using a slightly different approach: List rowCounts_2(IntegerMatrix x) { int n = x

Rcpp: Returning C array as NumericMatrix to R

余生颓废 提交于 2019-11-30 15:50:01
#include <Rcpp.h> #include <vector> extern "C" { #include "cheader.h" } using namespace Rcpp; // [[Rcpp::export]] NumericVector cppfunction(NumericVector inputR){ double const* input = inputR.begin(); size_t N = inputR.size(); double output[10*N]; cfunction(input, N, output); std::vector<double> outputR(output, output + sizeof(output) / sizeof(double)); return wrap(outputR); } This works except I have to manually convert the vector outputR to matrix in R. I could of course also make outputR to NumericMatrix (or can I?) and then return that but my real question is that is the above procedure