Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble building R package wtih devtools when it uses RcppArmadillo

This is my first stackoverflow question, so please be kind, folks!

I have greatly enjoyed my recently-found power to build R packages using devtools. However, as soon as I try building a package that uses RcppArmadillo, my workflow of running devtools::document(), devtools::check(), and devtools::build() no longer works.

For example, I have a (hopefully pretty minimal + complete) test version of the package I'm trying to develop here: https://github.com/suztolwinskiward/fooR/. fooR contains only one functions, which is a C++ implementation of the rdist.earth function from the fields package.

Running devtools::document("fooR") spits out lots of messages (several alluding to "undefined references" to variables that do not live in my source co that are not interpretable to me, and then fails:

    collect2: ld returned 1 exit status
    no DLL was created
    ERROR: compilation failed for package 'fooR'
    * removing 'C:/Users/I53794/AppData/Local/Temp/RtmpWgC8nD/devtools_install_1ea473123086/fooR'
    Error: Command failed (1)

One the other hand, when I source the C++ function that depends on RcppArmadillo, it seems to run just fine:

> Rcpp::sourceCpp('./src/rdist_earth_cpp.cpp')
> data('miami')
> data('new_orleans','katrina_path')
> rdist_earth_cpp(katrina_path,new_orleans)
            [,1]
 [1,] 1042.36073
 [2,]  998.96793
 [3,]  957.69315
 [4,]  917.91486
 [5,]  868.07791
 [6,]  805.73485
 [7,]  763.01476
 [8,]  726.10133
 [9,]  692.14482
[10,]  670.15133
[11,]  662.23353
[12,]  625.55592
[13,]  601.08682
[14,]  579.73940
[15,]  560.32660
[16,]  539.14192
[17,]  510.15438
[18,]  481.40037
[19,]  442.52322
[20,]  391.96619
[21,]  331.66378
[22,]  271.79088
[23,]  201.24749
[24,]  128.12647
[25,]   56.99198
[26,]   45.80297
[27,]   32.96609
[28,]   81.71237
[29,]  189.31050
[30,]  296.92104
[31,]  406.12593
[32,]  516.08458
[33,]  654.81113
[34,]  808.21670

This leads me to think there's something wrong with the way I'm trying to use RcppArmadillo in my package, but I haven't been able to figure out what. Any advice much appreciated!

P.S. I'm surprised there's no RcppArmadillo tag here....

like image 646
Suz Tolwinski-Ward Avatar asked Oct 28 '15 15:10

Suz Tolwinski-Ward


2 Answers

In addition to the answers of jtilly and the comment from Dirk:

RcppArmadillo.package.skeleton() generates the correct namespace file, but after running roxygen2 via document() the namespace just contains one line

# Generated by roxygen2: do not edit by hand

and the DynLib/export directives are overwritten. To let roxygen2 automatically generate the correct namespace, add a new R file to the R-subdirectory of your package directory containing the following:

#' @useDynLib YourPackageName
#' @importFrom Rcpp evalCpp
#' @exportPattern "^[[:alpha:]]+"
NULL

The name of this file doesn't matter, but YourPackageName.r is usual for this (kind of) "main file".

When running "document()", the following namespace file is generated:

# Generated by roxygen2: do not edit by hand

exportPattern("^[[:alpha:]]+")
importFrom(Rcpp,evalCpp)
useDynLib(YourPackageName)

This is the same namespace which is generated via RcppArmadillo.package.skeleton() by RcppArmadillo 0.6.700.6.0.

like image 147
Patrick Roocks Avatar answered Sep 28 '22 09:09

Patrick Roocks


Your NAMESPACE file is empty. It should contain something like this:

useDynLib(fooR)
exportPattern("^[[:alpha:]]+")
like image 21
jtilly Avatar answered Sep 28 '22 09:09

jtilly