Skip to content
Prev 6147 / 10988 Next

[Rcpp-devel] Seamless Rcpp gives errors with RcppArmadillo

Hello,

I don't have a copy of the book at hand. Below are some insights on what 
you are doing wrong.

Hope this helps.

Romain

Le 26/06/13 11:11, John Swan a ?crit :
This returns a colvec. See armadillo documentation:
http://arma.sourceforge.net/docs.html#sum
You need the "RcppArmadillo" plugin as you are using armadillo
You are calling rowSumsRcppArmadillo without argument, when it in fact 
takes an argument. This can not work.
This works for me, put this code in a cpp file:

#include <RcppArmadillo.h>
using namespace Rcpp ;
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::colvec rowSumsRcppArmadillo(NumericMatrix x){
     arma::mat X = arma::mat(x.begin(), x.nrow(), x.ncol(), false);
     return arma::sum(X, 1);
}


And then sourceCpp() it.

 > sourceCpp( "arma.cpp" )

 > x <- matrix(rep(1, 20), nrow = 4)

 > rowSumsRcppArmadillo(x)
      [,1]
[1,]    5
[2,]    5
[3,]    5
[4,]    5
Do you have RcppArmadillo installed ?
sourceCpp is for sourcing c++ files, not R files.