Setup an R-Package with Rcpp in RStudio

Image credit: John Moeses Bauan

How to make an R-Package with Rcpp in RStudio (updated 2020-07-19)

Prerequisites

Replace the old NAMESPACE file to allow Roxygen (see below) to handle it

> usethis::use_namespace(roxygen = TRUE)
Overwrite pre-existing file 'NAMESPACE'?

1: For sure
2: Nope
3: Not now

Selection: 1
✓ Writing 'NAMESPACE'

Replace the DESCRIPTION file to allow Roxygen (see below) to handle it

> usethis::use_description()
Overwrite pre-existing file 'DESCRIPTION'?

1: Negative
2: No way
3: I agree

Selection: 3
✓ Writing 'DESCRIPTION'
Package: myRcppPackage
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R (parsed):
    * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to
    pick a license
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
  • open the DESCRIPTION file and change it to your needs (e.g. change your name there)

Add Rcpp to the package

> usethis::use_rcpp()
✓ Creating 'src/'
✓ Adding '*.o', '*.so', '*.dll' to 'src/.gitignore'
● Copy and paste the following lines into '/home/rstudio/test/myRcppPackage/R/myRcppPackage-package.R':
  ## usethis namespace: start
  #' @useDynLib myRcppPackage, .registration = TRUE
  ## usethis namespace: end
  NULL
✓ Adding 'Rcpp' to LinkingTo field in DESCRIPTION
✓ Adding 'Rcpp' to Imports field in DESCRIPTION
● Copy and paste the following lines into '/home/rstudio/test/myRcppPackage/R/myRcppPackage-package.R':
  ## usethis namespace: start
  #' @importFrom Rcpp sourceCpp
  ## usethis namespace: end
  NULL
✓ Writing 'src/code.cpp'
● Modify 'src/code.cpp'
  • do every task marked with
  • You’ll need to add the R/*-package.R file manually. In this example, it contains the following lines:
## usethis namespace: start
#' @useDynLib myRcppPackage, .registration = TRUE
#' @importFrom Rcpp sourceCpp
## usethis namespace: end
NULL

Enable Roxygen

  • Build tab -> More -> Configure Build Tools:

Screenshot

  • Check Generate documentation with Roxygen:

Screenshot from 2020-02-05 09-47-44.png

  • now you can hit ‘Install and Restart’ to build your Package the first time

Add a license

> usethis::use_gpl3_license(name = "Sebastian Hanss")
✓ Setting active project to '/home/rstudio/test/myRcppPackage'
✓ Setting License field in DESCRIPTION to 'GPL-3'
✓ Writing 'LICENSE.md'
✓ Adding '^LICENSE\\.md$' to '.Rbuildignore'

Add a README.md

  • this is a description of your project
> usethis::use_readme_md()
✓ Writing 'README.md'
● Modify 'README.md'

Add Git version control

> usethis::use_git(message = "initial commit")
✓ Initialising Git repo
✓ Adding '.Rhistory', '.RData', '.Rproj.user' to '.gitignore'
There are 10 uncommitted files:
* '.gitignore'
* '.Rbuildignore'
* 'DESCRIPTION'
* 'LICENSE.md'
* 'man/'
* 'myRcppPackage.Rproj'
* 'NAMESPACE'
* 'R/'
* 'README.md'
* 'src/'
Is it ok to commit them?

1: I agree
2: Nope
3: Not now

Selection: 1
✓ Adding files
✓ Commit with message 'initial commit'
● A restart of RStudio is required to activate the Git pane
Restart now?

1: No way
2: Yeah
3: Nope

Selection: 2

Add your repository to GitHub

  • you need to get a personal access token (PAT) to enable usethis to create a repository at GitHub Screenshot from 2020-02-05 09-47-44.png
  • change protocol to "ssh" when you have set-up GitHub with a SSH keys (which you should do)
> usethis::use_github(private = TRUE, protocol = "https", auth_token = 'your-personal-access-token')
✓ Checking that current branch is 'master'
● Check title and description
  Name:        myRcppPackage
  Description: What the Package Does (One Line, Title Case)
Are title and description ok?

1: Definitely
2: Negative
3: Nope

Selection: 1
✓ Creating GitHub repository
✓ Setting remote 'origin' to 'git@github.com:bitbacchus/myRcppPackage.git'
✓ Adding GitHub links to DESCRIPTION
✓ Pushing 'master' branch to GitHub and setting remote tracking branch
● Failed to push and set tracking branch.
  This often indicates a problem with git2r and the credentials.
  Try this in the shell, to complete the set up:
  `git push --set-upstream origin master`
✓ Opening URL 'https://github.com/bitbacchus/myRcppPackage'
  • there’s a small hicc-up (● Failed to push ...) - just switch to the terminal tab and enter git push --set-upstream origin master

Screenshot from 2020-02-05 09-47-44.png

Sources and further reading

Avatar
Sebastian Hanß
PhD student & Scientific programmer

spatial ecology, biodiversity and ecosystem functioning, savanna and dryland ecology, concepts and theoretical ecology and dynamic simulation models.