6

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....

4 Answers4

6

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.

Patrick Roocks
  • 3,129
  • 3
  • 14
  • 28
3

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

useDynLib(fooR)
exportPattern("^[[:alpha:]]+")
jtilly
  • 493
  • 2
  • 7
  • 2
    ... and it would be created for you if you used tools like `RcppArmadillo.package.skeleton()` which we created **precisely** to aid novices in package creation. – Dirk Eddelbuettel Oct 28 '15 at 16:42
  • 1
    Thanks to you both. I will definitely learn RcppArmadillo.packge.skeleton()-- thanks for developing it Dirk et al. Usually devtools::document() builds the namespace for me, so I don't know why that avenue doesn't seem to work as soon as I include RcppArmadillo. – Suz Tolwinski-Ward Oct 28 '15 at 17:10
1

What eventually worked was to initialize a new package with RcppArmadillo.skeleton.package, move all my previous files therein, document manually, and then check and build with the GUI buttons in RStudio. This feels pretty kludgy and I really liked using roxygen2 much better for documentation-- but as a relative novice in package development with RcppAmadillo dependence, I am just happy for now to have found a way to build successfully!

0

'devtools::document()' compiles your code, honestly, I'm not sure why. This means that if the compilation fails, the documentation isn't completed. It seems in your case this means you don't get your NAMESPACE written.

Start as Dirk suggests, and add stuff in, but it will have to compile before the docs get done.

Jack Wasey
  • 3,360
  • 24
  • 43
  • I did start as Dirk suggested, but when I use devtools::document(), I get a warning `NAMESPACE not generated by roxygen2. Skipped.` and then a statement that the DLL was not loaded. So not sure how to reconcile NAMESPACE from RcppArmadillo.skeleton.package with continued package development using devtools (which offers lots of other features I like and with which I have had success until needing fast linear algebra!) – Suz Tolwinski-Ward Oct 28 '15 at 20:45