Search code examples
rpackagerstudiodevtools

Creating an R package with devtools and the function "use_rcpp" is instructing me to copy lines of code into a file that does not exist


I'm attempting to create a small test R package just to get a hang of the workflow. I've been following the tutorial found here: https://r-pkgs.org/index.html Everything has been running smoothly but eventually I want to add rcpp code to my package and this is where I'm running into problems. The function use_rcpp in the devtools package is supposed to set up your package with everything you need to start adding c++ files. However once the command is done running it tells you to copy and paste comments into a file that does not exist. The tutorial I'm following says the same thing here https://r-pkgs.org/src.html#cpp but just says to add the roxygen tags to your package. It doesn't specify where I need to add them.

The output I'm getting is different from the one in the tutorial but I assume thats just because im running a different version.

The output the function is giving:

✔ Setting active project to 'R/packages/rcppTester'

✔ Creating 'src/'

✔ Adding '.o', '.so', '*.dll' to 'src/.gitignore'

● Copy and paste the following lines into 'R/packages/rcppTester/R/rcppTester-package.R':

## usethis namespace: start

#' @useDynLib rcppTester, .registration = TRUE

## usethis namespace: end

NULL

[Copied to clipboard]

✔ Adding 'Rcpp' to LinkingTo field in DESCRIPTION

✔ Adding 'Rcpp' to Imports field in DESCRIPTION

● Copy and paste the following lines into 'R/packages/rcppTester/R/rcppTester-package.R':

## usethis namespace: start

#' @importFrom Rcpp sourceCpp

## usethis namespace: end

NULL

[Copied to clipboard]

Again I have no file named "rcppTester-package.R" and my package is throwing errors when I try and use the cpp files. Any advice would be greatly appreciated.


Solution

  • You do not have to use devtools or usethis if they confuse you -- they do add a layer of obfuscation that sometimes obstruct what is happening.

    Rcpp itself has a function Rcpp.package.skeleton(), and the RStudio IDE has something related (but independent) under 'File -> New Project -> Package with Rcpp'. I use both. You could start with either and get a package that compiles. Both will do that.

    Take that as a snapshot and then add roxygen2 documentation. If you do it manually, you need to run compileAttributes() to get the roxygen markup from the C++ file to the R file where roxygenize() will see it. RStudio does both for you automatically.

    So to sum up: try separating 'package with Rcpp' and 'package using roxygen' if the joint 'package using Rcpp and roxygen' gives you issues.