Building R Packages

Hands On

Load Required Packages
# devtools automates some of the setup for a new package

# roxygen2 handles building the documentation
Define Some Variables
# Define package directory
package_wd <- "/home/mk5636/badas-r-package"

# Define package name as string
package_name <- 'badas'
Create A Package Skeleton Using create()

# The 'create' function from devtools creates the
# directory structures and skeleton help files
# for a new package
Examine at the directory structure and DESCRIPTION file that was created by the create() function

Create a Function

Create an R function with a roxygen2-style header (for documentation). This will be incorporated into the package.
#' custom_add
#' A custom function to add two numbers together
#' @name custom_add
#' @param x The first number.
#' @param y The second number.
#' @return The result of adding the two numbers.
#' @export
#' @examples
#' result <- custom_add(1,2)
custom_add <- function(x,y){
Let’s test it out
custom_add(1, 2)
## [1] 3
Copy and paste the function into a file called {function_name}.R in {package_wd}/R/

Add data to a function

# Name the data
data_name <- 'mydata'

# Create the data object
data_obj <- c(1,2)

# Assign the data name to the data object
assign(x=data_name, value=data_obj)

# Create the data folder
data_path <- paste(package_wd, "/", package_name, "/data/" ,sep="")

# Write .rda file to data folder
data_file <- paste(data_path, data_name, ".rda", sep="")
save(list=data_name, file=data_file)
Create the documentation for the data
#' mydata
#' A vector containing some test data.
#' @format A vector with 2 values: c(1,2)
#' @source \\url{}
Copy and paste the above into a file called {data_name}.R in {package_wd}/R/

Document, Build, and Install the Package


# The 'document' function from devtools is a wrapper for the
# roxygen2::roxygenize() function from the roxygen2 package
# It builds all documentation for a package
# The 'build' function from devtools converts a
# package source directory into a single bundled file. 
# Returns the path to the built package
package_tar <- build(package_name)
# Let's install the package now
install.packages(package_tar, repos=NULL, type='source')

Quit your R session, start a new session, and try the following commands

This shouldn’t work
Error in custom_add(1, 2) : could not find function "custom_add"
This should work:
## [1] 3
See how the roxygen2 header we created for the function translates into documentation

Congratulations, you have successfully built an R package! Now you can share the tarball with collaborators to allow them to use your package. Next we will learn about how to submit packages to CRAN and how to install packages from GitHub.

Submitting to CRAN

Submitting to CRAN used to be very hard: creating documentation by hand, manually checking everything, etc. But with tools like devtools, roxygen2, etc. it has become much easier.

1) Might be a good idea to check the package name you want to use is not already taken. You can search the CRAN package list, or you can use the package available.

available("available", browse = FALSE)
You can also use suggest to help you come up with a name, ex:
suggest("Convert Yeast IDs")
## [1] convertr
You probably want to do this before you build your package.

2) Run the check_built() function to ensure:

  • package can be built, installed, and uninstalled without issues
  • no potential conflicts, plays nice with other packages
  • required documentation present
  • correct formatting
  • no warnings
  • no notes
If there are notes, you need to explain them in the comments section when you submit your package.

3) Once you’re ready, submission is done through the web:

Upload the tarball, which must have been built using the build() function as we did, along with your name and email address, and comments optionally (here is where you would explain any notes from the check_built() function for example).

Please note: You should read the policies and ensure your package is compliant prior to submitting:

Installing Packages from GitHub


Leave a Reply

Your email address will not be published. Required fields are marked *