Troubleshooting R package with devtools when it uses RcppArmadillo

This is my first stack question, so please be kind guys!

I really liked my recently found power to build R packages using devtools. However, as soon as I try to create a package that uses RcppArmadillo, my workflow with running devtools :: document (), devtools :: check () and devtools :: build () no longer works.

For example, I have (I hope, quite minimal + full) a test version of the package that I am trying to create here: https://github.com/suztolwinskiward/fooR/ . fooR contains only one function, which is an implementation of the rdist.earth function from C ++ from the field package.

Running devtools :: document ("fooR") spills out a lot of messages (several referring to "undefined references" to variables that do not live in my original ko, which are not interpreted to me, and then are not executed:

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) 

On the other hand, when I start from a C ++ function that depends on RcppArmadillo, it seems to work 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 

It makes me think that something is wrong with the way I try to use RcppArmadillo in my package, but I could not understand what. Any advice is greatly appreciated!

PS I am surprised that there is no RcppArmadillo tag.

+5
source share
4 answers

In addition to jtilly's answers and comment from Dirk:

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

 # Generated by roxygen2: do not edit by hand 

and the DynLib / export directives are overwritten. For roxygen2 to 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 does not matter, but YourPackageName.r usually used for this (kind of) "main file".

When you run "document ()", the following namespace file is created:

 # Generated by roxygen2: do not edit by hand exportPattern("^[[:alpha:]]+") importFrom(Rcpp,evalCpp) useDynLib(YourPackageName) 

This is the same namespace that is generated through RcppArmadillo.package.skeleton() by RcppArmadillo 0.6.700.6.0.

+3
source

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

 useDynLib(fooR) exportPattern("^[[:alpha:]]+") 
+2
source

'devtools :: document ()' compiles your code, to be honest, I'm not sure why. This means that if compilation fails, the documentation will not be completed. It seems in your case that means that you have not received your NAMESPACE.

Start with what Dirk suggests and add material, but it will need to be compiled before the documents are executed.

0
source

I ended up working on initializing a new package using RcppArmadillo.skeleton.package, move all of my previous files in it, manually, and then check and create using the GUI buttons in RStudio. This is very unpleasant, and I really liked using roxygen2 much better for documentation - but as a relative newbie in developing packages with RcppAmadillo dependency, I'm just happy that I found a way to build it successfully!

0
source

Source: https://habr.com/ru/post/1234713/


All Articles