Developing R Packages
Gareth Burns
Exploristics
July 17, 2023
Developing an R Package 📦
An R package 📦 is a collection of functions that have a shared purpose. Installing base R comes with existing core packages (e.g. stats), and tens of thousands on additional packages on additional code repositories CRAN, GitHub, Bioconductor.
We’re often using other people packages (every time we use library) - so by replacing source calls within our scripts we’re not changing our workflow, we’re just need new skills and techniques to develop packages.
Aim: The aim of the presentation today is to provide the simplest workflow to take a collection of existing functions and turn them into an R package 📦.
Why develop an R package?
Collects shared code/functions in a single source
Modularises code
Easier to re-use individual components
Easier to maintain
Encourages best practice
Documentation
File Structure
Validation
Why develop an R package?
Easier to share and collaborate
- Defined processes and structure
- Can store on code repositories
Scale-able
KerusCore is an R package run on AWS
DMB is an R package deployed in a Container on AWS
Allows access to other tools/services
Unit testing
Continuous Integration/ Development
Other packages can build off your packages
What does an R Package 📦 consist of?
Description File
This contains meta-data about the package
Package: mypackage
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "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
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
See https://r-pkgs.org/description.html#description
Namespace File
Defines the dependencies (what packages it imports/uses) and what is exported from the package (i.e. the functions you create which a user can access).
Using the :: operator is a way of accessing what is in the namespace for a package (e.g. dplyr::filter() ).
The namespace gets more complicated with OOP and C++ but it outside scope of current presentation.
See https://r-pkgs.org/namespace.html#namespace
How to share package
GitHub
devtools::install_github("GABurns/ShortsWeatheR")
If not public ensure permission has been granted
Share internally
Build the package into a source / tar file. This can be done using RStudio
- Build > More > Build Source Package
Put the tar file in a shared network location or email to collaborator. Users can either download the tar file and then install the package, or install the package directly from the compressed folder on the shared drive
install.packages("path/to/tar/file", source = TRUE, repos = NULL)
Use library(packageName)