add and Rcpp file to an existing r Package?
Asked Answered
A

1

6

I have already made a simple R package (pure R) to solve a problem with brute force then I tried to faster the code by writing the Rcpp script. I wrote a script to compare the running time with the "bench" library. now, how can I add this script to my package? I tried to add

#'@importFrom Rcpp cppFunction 

on top of my R script and inserting the Rcpp file in the scr folder but didn't work. Is there a way to add it to my r package without creating the package from scratch? sorry if it has already been asked but I am new to all this and completely lost.

Abott answered 11/10, 2020 at 15:17 Comment(1)
You can have a look at privefl.github.io/advr38book/packages.html#rcpp.Hebraist
S
9

That conversion is actually (still) surprisingly difficult (in the sense of requiring more than just one file). It is easy to overlook details. Let me walk you through why.

Let us assume for a second that you started a working package using the R package package.skeleton(). That is the simplest and most general case. The package will work (yet have warning, see my pkgKitten package for a wrapper than cleans up, and a dozen other package helping functions and packages on CRAN). Note in particular that I have said nothing about roxygen2 which at this point is a just an added complication so let's focus on just .Rd files.

You can now contrast your simplest package with one built by and for Rcpp, namely by using Rcpp.package.skeleton(). You will see at least these differences in

  • DESCRIPTION for LinkingTo: and Imports
  • NAMESPACE for importFrom as well as the useDynLib line
  • a new src directory and a possible need for src/Makevars

All of which make it easier to (basically) start a new package via Rcpp.package.skeleton() and copy your existing package code into that package. We simply do not have a conversion helper. I still do the "manual conversion" you tried every now and then, and even I need a try or two and I have seen all the error messages a few times over...

So even if you don't want to "copy everything over" I think the simplest way is to

  • create two packages with and without Rcpp
  • do a recursive diff
  • ensure the difference is applied in your original package.

PS And remember that when you use roxygen2 and have documentation in the src/ directory to always first run Rcpp::compileAttributes() before running roxygen2::roxygenize(). RStudio and other helpers do that for you but it is still easy to forget...

Suborbital answered 11/10, 2020 at 15:28 Comment(2)
Glad to have been of help. There are other helper wrappers (and I wrote a few of my own) but I feel it is important for anything more than casual use to know and understand what is happened. And working from small yet complete examples does that.Suborbital
Wondering what the name of these helper wrappers are. Or if there is now an updated method in which to add on Rcpp to an existing R package. Otherwise I am going to try some examples and the solution you suggested. Thanks.Thievery

© 2022 - 2024 — McMap. All rights reserved.