The following outlines the basic workflow for building, testing and extending R packages with the package development environment functionalities outlined above.
(a) Create package skeleton
library("devtools"); library("roxygen2"); library("usethis"); library(sinew) # If not availble install these packages with 'install.packages(...)'
create("myfirstpkg") # Creates package skeleton. The chosen name (here myfirstpkg) will be the name of the package.
setwd("myfirstpkg") # Set working directory of R session to package directory 'myfirstpkg'
use_mit_license() # Add license information to description file (here MIT). To look up alternatives, do ?use_mit_license
(b) Add R functions
Next, R functions can be added to *.R
file(s) under the R directory of the new package. Several functions can be organized in one *.R
file, each in its own file or any combination. For demonstration purposes, the following will download an R file (pkg_build_fct.R
from here) defining two functions (named:myMAcomp
and talkToMe
) and save it to the R directory of the package.
[ Scroll down to continue ]
download.file("https://raw.githubusercontent.com/tgirke/GEN242/main/content/en/tutorials/rpackages/helper_functions/pkg_build_fct.R", "R/pkg_build_fct.R")
(c) Auto-generate roxygen comment lines
The makeOxygen
function from the sinew
package creates roxygen2
comment skeletons based on the information from each function (below for myMAcomp
example). The roxygen comment lines need to be added above the code of each function. This can be done by copy and paste from the R console or by writing the output to a temporary file (below via writeLines
). Alternatively, the makeOxyFile
function can be used to create a roxygenized copy of an R source file, where the roxygen comment lines have been added above all functions automatically. Next, the default text in the comment lines needs to be replaced by meaningful text describing the utility and usage of each function. This editing process of documentation can be completed and/or revised any time.
load_all() # Loads package in a simulated way without installing it.
writeLines(makeOxygen(myMAcomp), "myroxylines") # This creates a 'myroxylines' file in current directory. Delete this file after adding its content to the corresponding functions.
(d) Autogenerate help files
The document
function autogenerates for each function one *.Rd
file in the man
directory of the package. The content in the *.Rd
help files is based on the information in the roxygen comment lines generated in the previous step. In addition, all relevant export/import instructions are added to the NAMESPACE
file. Importantly, when using roxygen-based documentation in a package then the NAMESPACE
and *.Rd
files should not be manually edited since this information will be lost during the automation routines provided by roxygen2
.
document() # Auto-generates/updates *.Rd files under man directory (here: myMAcomp.Rd and talkToMe.Rd)
tools::Rd2txt("man/myMAcomp.Rd") # Renders Rd file from source
tools::checkRd("man/myMAcomp.Rd") # Checks Rd file for problems
(e) Add a vignette
A vignette template can be auto-generated with the use_vignette
function from the usethis
package. The *.Rmd
source file of the vignette will be located under a new vignette
directory. Additional vignettes can be manually added to this directory as needed.
use_vignette("introduction", title="Introduction to this package")
(f) Check, install and build package
Now the package can be checked for problems. All warnings and errors should be addressed prior to submission to a public repository. After this it can be installed on a user’s system with the install
command. In addition, the build
function allows to assemble the package in a *.tar.gz
file. The latter is often important for sharing packages and/or submitting them to public repositories.
setwd("..") # Redirect R session to parent directory
check("myfirstpkg") # Check package for problems, when in pkg dir one can just use check()
# remove.packages("myfirstpkg") # Optional. Removes test package if already installed
install("myfirstpkg", build_vignettes=TRUE) # Installs package
build("myfirstpkg") # Creates *.tar.gz file for package required to for submission to CRAN/Bioc
(g) Using the new package
After installing and loading the package its functions, help files and vignettes can be accessed as follows.
library("myfirstpkg")
library(help="myfirstpkg")
?myMAcomp
vignette("introduction", "myfirstpkg")
Another very useful development function is test
for evaluating the test code of a package.
(h) Share package on GitHub
To host and share the new package myfirstpkg
on GitHub, one can use the following steps:
- Create an empty target GitHub repos online (e.g. named
mypkg_repos
) as outlined here.
- Clone the new GitHub repos to local system with
git clone https://github.com/<github_username>/<repo name>
(here from command-line)
- Copy the root directory of the package into the downloaded repos with
cp myfirstpkg mypkg_repos
- Next
cd
into mypkg_repos
, and then add all files and directories of the package to the staging area with git add -A :/
.
- Commit and push the changes to GitHub with:
git commit -am "first commit"; git push
.
- After this the package should be life on the corresponding GitHub web page.
- Assuming the package is public, it can be installed directly from GitHub by anyone as shown below (from within R). Installs of private packages require a personal access token (PAT) that needs to be assigned to the
auth_token
argument. PATs can be created here.
devtools::install_github("<github_user_name>/<mypkg_repos>", subdir="myfirstpkg") # If the package is in the root directory of the repos, then the 'subdir' argument can be dropped.