library(tidyverse)
library(plotly)Developing Your Own R Package
A casual demonstration of modern practices for R package development.
This note is available at https://rpubs.com/emitanaka/anu-rpkg-dev.
Do you have some code or functions in R that you would like to make and distribute as an R package? Emi will demonstrate some modern practices for R package development and, where time permits, the process of publishing your package on CRAN. The demonstration will be casual and those coming are free to ask questions or share their own tips or tribulations in developing or publishing an R package.
Recognizing software as a first-class research output is a national agenda and there is a clear wave of change coming that will change the research landscape.
There are a number of awards available for research software development that you can consider applying for.
National
- The Di Cook Award for student developers in Australia
- The Venables Award for new developers in Australia
- The ARDC Award for New Developers of Open Source Software in Ecology
- The Eureka Prize for Excellence in Research Software
- The Torsten Seemann Outstanding Bioinformatics Software Developer Award
- The Outstanding Bioinformatics Software Maintainer Award
- The Emerging Leaders in Astronomy Software Development Prize
Global
- John M. Chambers Statistical Software Award for students
- William D. Richards, Jr. Software Award for social network analysis software
What are some of your favorite packages?
What is an R-package?
A container:
- for a set of R functions,
- to share data,
- to share an app,
- and more, e.g. Rmd templates,
Motivation
- What does your package (and functions) do?
- Who is it for?
- Why use it?
- Where do we find and install it?
- How do we use it?
đź’ˇ Idea
Let’s make a simple set of arithmetic functions.
The user interface would look like:
add(1, 3) # 1 + 3
subtract(5, 3) # 5 - 3
divide(6, 3) # 6 / 3
multiply(3, 2) # 3 * 2Functions
add <- function(x, y) x + y
subtract <- function(x, y) x - y
divide <- function(x, y) x / y
multiply <- function(x, y) x * yR-package development helper packages
devtoolsusethisroxygen2pkgdowntestthat
install.packages(c("devtools", "usethis", "roxygen2", "pkgdown", "testthat"))Anatomy of an R-package
DESCRIPTIONfileR/directory for R files that contain your functionsNAMESPACEfile (you don’t need to manually create this)
Optionally,
data/for binary data available to the userdata-raw/for raw datainst/for arbitrary additional files that you want include in your package- and so on.
DESCRIPTION file
- Metadata for the package
- Package name
- Title and description
- Authors
- Dependencies (depends, imports, suggests)
- Licensing
- Version number
- Bug report location, and so on.
Creating an R-package
available::available("maths") # check if package is available
usethis::create_package("maths")
usethis::use_r("new-r-file") # creating new R file in the R/ directory
# add R functions to the R file
devtools::load_all()Documenting R functions with roxygen2
use
#'above a function to write documentation for that functionroxygen2uses@tags to structure documentation, e.g.Â- any text after
@descriptionis the description - any text after
@paramdescribes the arguments of the function @exportsignals that it is an exported function- any text after
@returndescribes the return object
- any text after
The full list of Rd tags are found at https://roxygen2.r-lib.org/articles/rd.html
Then
devtools::document()converts the Rd tags to appropriate sections of.Rdfiles written in theman/folder
devtools::document()Unit testing with testthat
usethis::use_test()- This creates a file
test-active-filename.Rintests/testthat/directory
test_that("operations works", {
expect_equal(add(3, 4), 3 + 4)
expect_equal(subtract(3, 4), 3 - 4)
expect_equal(divide(3, 4), 3 / 4)
expect_equal(multiply(3, 4), 3 * 4)
})devtools::test_active_file()
devtools::test()Master the keyboard shortcuts
- Cmd/Ctrl + Shift + L: Load all
- Cmd/Ctrl + Shift + D: Document
- Cmd/Ctrl + Shift + T: Test
- Cmd/Ctrl + Shift + B: Build and Reload
- plus more… see RStudio IDE > Tools > Keyboard Shortcuts Help
Whole R package development workflow
available::available("pkgname") # check if package name is available (if planning to publish publicly)
usethis::create_package("pkgname")
usethis::use_git() # set up version control
usethis::use_github() # optional
usethis::use_r("myfile")
# write some functions in a script
usethis::use_data_raw() # if adding data
devtools::load_all() # try it out in the console
usethis::use_package("import-pkgname") # add package to import (or depends or suggests)
usethis::use_package_doc() # add package documentation
usethis::use_pipe() # if you want to add %>% from `magrittr`
usethis::use_vignette("vignette-name") # add vignette
usethis::use_test() # make test file for active R file
# write some test
devtools::test_active_file() # test active file
devtools::test() # test whole package
devtools::build() # build vignettes
devtools::install() # to install package
devtools::check() # to build and check a package
usethis::use_readme_rmd() # to add a README Rmd file
styler::style_pkg() # optional (commit first, then review changes carefully)
usethis::use_pkgdown_github_pages() # for setting up pkgdown website on github
# `usethis::use_pkgdown()` if not using github pagesUseful reference
- R Packages book (2e) https://r-pkgs.org/
- rOpenSci Statistical Software Peer Review https://stats-devguide.ropensci.org/
Publishing software
Some outlets include:
Domain-specific software also get published in the domain journals, e.g. Bioinformatics, Methods in Ecology and Evolution, and so on.