Hao Ye Health Science Center Libraries, University of Florida (updated: 2022-10-03)
roxygen2
pkgdown
, testthat
)Setup Before getting started, there are some tools to install to make creating packages easier.
I recommend using RStudio
as a development environment.
Creating a new R package
devtools::create("myDemoPkg")
myDemoPkg ├── .gitignore ├── .Rbuildignore ├── DESCRIPTION ├── myDemoPkg.Rproj ├── NAMESPACE └── R/
What are these files?
.gitignore
tells git to NOT track certain files.Rbuildignore
tells R to NOT include certain files when building the packageDESCRIPTION
contains basic metadatamyDemoPkg.Rproj
is the RStudio projectNAMESPACE
lists the objects to load with the packageR/
contains the R codeBuilding the Package
R Console
devtools::install()
OR
RStudio Build Pane
Demo
Fully Operational!
note: other cloud services exist (e.g. GitLab) – we restrict ourself to GitHub in this course
Cloud-based backup of your new package
Supports installation from R
remotes::install_github("{username}/{repo}")
# remotes::install_github("ha0ye/myDemoPkg")
Install Git and register a GitHub account - https://happygitwithr.com/install-intro.html
Establish a Git repo for the project and make an initial commit
usethis::use_git()
usethis
)Using a personal access token (PAT) that has permissions to create repos
usethis::use_github()
(see https://usethis.r-lib.org/reference/github-token.html for more info on tokens) OR
origin
for the “Remote Name”
usethis::use_mit_license()
base
package, need to be listed as dependencies.{pkg}::{fun}
.use_package
function will add a package to the dependencies in DESCRIPTION
.
usethis::use_package("utils")
R/
folder.
f <- function(df)
{
names(df)
}
NAMESPACE
needs to include the names of objects to be loaded alongside the package.NAMESPACE
file directly, it is preferable to use devtools
and roxygen2
to create NAMESPACE
for us.devtools::document()
to generate the documentation files.#' @export
right before an object will include it in NAMESPACE
:
#' @export
f <- function(df)
{
names(df)
}
usethis::use_data()
to export a dataset to a file and add that file to the package:
dat <- data.frame(x = 1:3, y = 5:7)
usethis::use_data(dat) # no quotes
roxygen2
.rd
, and stored in the man/
folderroxygen2
adopts the idea of doxygen
for R:Example
#' Get the column names of a data.frame
#' @param df A data.frame
#' @return a character vector
#' @export
f <- function(df)
{
names(df)
}
Data
data.R
file:
#' Example data.frame
#'
#' @format A data frame with 3 rows and 2 variables:
#' \describe{
#' \item{x}{some numbers}
#' \item{y}{some other numbers}
#' }
"dat"
testthat
pkgdown
usethis::use_github_action("check-release")