Skip to content

Instantly share code, notes, and snippets.

@mryap
Last active May 6, 2021 07:43
Show Gist options
  • Select an option

  • Save mryap/fa31a3bd765eb9c228480009ee6a359b to your computer and use it in GitHub Desktop.

Select an option

Save mryap/fa31a3bd765eb9c228480009ee6a359b to your computer and use it in GitHub Desktop.
Building and installing an R package

Building and installing an R package

If you’re using Windows, you’ll want to install Rtools.

using devtools

use Hadley Wickham’s devtools package. You can then build and install the package from within R.

Start R within your package directory (so that your package directory is R’s working directory).

Install devtools by typing (within R)

install.packages(devtools)

Then load the devtools package with

library(devtools)

Then, to build the package, type

# Hello, world!
#
# This is an example function named 'hello'
# which prints 'Hello, world!'.
#
# You can learn more about package authoring with RStudio at:
#
# http://r-pkgs.had.co.nz/
#
# Some useful keyboard shortcuts for package authoring:
#
# Install Package: 'Ctrl + Shift + B'
# Check Package: 'Ctrl + Shift + E'
# Test Package: 'Ctrl + Shift + T'
# 1. create a data directory to store the data for this package
# library(devtools)
# usethis::use_data_raw() # This will create a data-raw/ subdirectory
# 2.
# Open up the newly created DESCRIPTION file in the root of the package directory.
# You will want to make a few changes to this file.
# 3.
#
#
# 4.
# Documentation
# Create a .R file with Roxygen2 comments in the R subdirectory.
# 5.
# In RStudio, go to Build -> Check Package.
# RStudio will run a variety of checks on your package.
# 6.
# Once checks complete, push your local repository to remote git repository.
# References
# https://www.erikhoward.net/blog/how-to-create-an-r-data-package/
# https://teachdatascience.com/datapackage/
# https://grasshoppermouse.github.io/2017/10/18/put-your-data-in-an-r-package/
# https://towardsdatascience.com/put-your-data-analysis-in-an-r-package-even-if-you-dont-publish-it-64f2bb8fd791

Remove that minimal NAMESPACEfile from your R package and add Roxygen2 comments to your R code.

Use the document() function in devtools to process the Roxygen2 comments and so create the NAMESPACE file and the .Rd files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment