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 :
Hi,
I just purchased Dirk's book on Seamless Rcpp and I'm finding it very
useful.
However, I'm having a strange error running RcppArmadillo inline, where
the book examples seem to manage it effortlessly.
I've been stuck on this for hours. Any help is most appreciated.
I am using R 3.0 on Mac OS X Mountain Lion 10.8.4, sessionInfo output:
> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_GB.UTF-8/C/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] inline_0.3.12 RcppArmadillo_0.3.900.0 Rcpp_0.10.3
BDgraph_2.6 huge_1.2.4 MASS_7.3-26
igraph_0.6.5-2
[8] Matrix_1.0-12 lattice_0.20-15
loaded via a namespace (and not attached):
[1] grid_3.0.1 tools_3.0.1
I have the following in a source file (somefile.R):
library(Rcpp)
library(inline)
library(RcppArmadillo)
rowSumsRcppArmadilloFunction <- '
arma::rowvec rowSumsRcppArmadillo(NumericMatrix x){
arma::mat X = arma::mat(x.begin(), x.nrow(), x.ncol(), false);
return arma::sum(X, 1);
This returns a colvec. See armadillo documentation:
http://arma.sourceforge.net/docs.html#sum
}'
rowSumsRcppArmadillo <- cxxfunction(signature(),
plugin="Rcpp",
You need the "RcppArmadillo" plugin as you are using armadillo
incl=rowSumsRcppArmadilloFunction,
body='
return Rcpp::wrap(rowSumsRcppArmadillo());
')
You are calling rowSumsRcppArmadillo without argument, when it in fact
takes an argument. This can not work.
If I try to execute this in R console with the source command, I get:
> source("/Users/me/Desktop/R/somefile.R")
file29967eb06d23.cpp:20: error: ‘arma’ has not been declared
file29967eb06d23.cpp:20: error: expected constructor, destructor, or
type conversion before ‘rowSumsRcppArmadillo’
file29967eb06d23.cpp: In function ‘SEXPREC* file29967eb06d23()’:
file29967eb06d23.cpp:35: error: ‘rowSumsRcppArmadillo’ was not declared
in this scope
make: *** [file29967eb06d23.o] Error 1
ERROR(s) during compilation: source code errors or compiler
configuration errors!
Program source:
1:
2: // includes from the plugin
3:
4: #include <Rcpp.h>
5:
6:
7: #ifndef BEGIN_RCPP
8: #define BEGIN_RCPP
9: #endif
10:
11: #ifndef END_RCPP
12: #define END_RCPP
13: #endif
14:
15: using namespace Rcpp;
16:
17:
18: // user includes
19:
20: arma::rowvec rowSumsRcppArmadillo(NumericMatrix x){
21: arma::mat X = arma::mat(x.begin(), x.nrow(), x.ncol(), false);
22: return arma::sum(X, 1);
23: }
24:
25: // declarations
26: extern "C" {
27: SEXP file29967eb06d23( ) ;
28: }
29:
30: // definition
31:
32: SEXP file29967eb06d23( ){
33: BEGIN_RCPP
34:
35: return Rcpp::wrap(rowSumsRcppArmadillo());
36:
37: END_RCPP
38: }
39:
40:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!
file29967eb06d23.cpp:20: error: ‘arma’ has not been declared
file29967eb06d23.cpp:20: error: expected constructor, destructor, or
type conversion before ‘rowSumsRcppArmadillo’
file29967eb06d23.cpp: In function ‘SEXPREC* file29967eb06d23()’:
file29967eb06d23.cpp:35: error: ‘rowSumsRcppArmadillo’ was not declared
in this scope
make: *** [file29967eb06d23.o] Error 1
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD
SHLIB file29967eb06d23.cpp 2> file29967eb06d23.cpp.err.txt' had status 1
I added these:
20: #include <RcppArmadillo.h>
21: // [[Rcpp::depends(RcppArmadillo)]]
22:
23: // [[Rcpp::export]]
24:
25: arma::rowvec rowSumsRcppArmadillo(NumericMatrix x){
26: arma::mat X = arma::mat(x.begin(), x.nrow(), x.ncol(), false);
27: return arma::sum(X, 1);
28: }
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
But then I just get this error:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!
file77e03dcff275.cpp:20:27: error: RcppArmadillo.h: No such file or
directory
Do you have RcppArmadillo installed ?
file77e03dcff275.cpp:25: error: ‘arma’ has not been declared
file77e03dcff275.cpp:25: error: expected constructor, destructor, or
type conversion before ‘rowSumsRcppArmadillo’
file77e03dcff275.cpp: In function ‘SEXPREC* file77e03dcff275()’:
file77e03dcff275.cpp:40: error: ‘rowSumsRcppArmadillo’ was not declared
in this scope
make: *** [file77e03dcff275.o] Error 1
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD
SHLIB file77e03dcff275.cpp 2> file77e03dcff275.cpp.err.txt' had status 1
So, I thought I'd try compilng it as a package, but when I try to call
the package in R, I get errors like:
Error in sourceCpp("./R/somefile.R") :
Error 1 occurred building shared library.
sourceCpp is for sourcing c++ files, not R files.
WARNING: The tools required to build C++ code for R were not found.
Please install Command Line Tools for XCode (or equivalent).
ld: warning: directory not found for option
'-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/x86_64'
ld: warning: directory not found for option
'-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3'
ld: library not found for -lgfortran
collect2: ld returned 1 exit status
make: *** [sourceCpp_13326.so] Error 1
The XCode command line tools *are* installed - I'm an iOS developer so I
always make sure that they are there:
John-MBPR:BDGraphSource johnswan$ which llvm-g++
/usr/bin/llvm-g++
John-MBPR:BDGraphSource johnswan$ llvm-g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build
5658) (LLVM build 2336.11.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Can anyone help?
Thanks
John
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://gallery.r-enthusiasts.com
blog: http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel