There are lots of ways to create an R package, and there’s lots of documentation on the process. This file isn’t a substitute for you reading in more detail, but it’s a quick way to get up and running with a minimal package environment. I think it’s helpful to start from the most basic, built-in method of creating an R package template and building up to use newer and more advanced tools.

Step 1: Install some packages we’ll use later

install.packages(c("devtools", "roxygen2", "testthat", "knitr"))

Step 2: Define a dummy function to add to R package.

You can define whatever else you’d like at this point, but the idea here is to get a working package template - not to have all the package functionality we ultimately want.

matmult <- function(A, B){
  A %*% B
}

Step 3: Create the package skeleton

You should call your package something else - I’m going to be updating the BIOS7730 package throughout the course, and you may want to keep a copy synced up.

package.skeleton(name = "BIOS7330")

The above function is built into R - you can also use the devtools::create function, but I don’t like how integrated/dependent this tool has become with/on Rstudio (personal preference).

Step 4: Update the DESCRIPTION file using your text editor of choice

# Example to open a folder on Windows so you can edit the file
system("cmd.exe", input = "explorer ./BIOS7330")

We’ll see a lot more of the DESCRIPTION file later on.

One thing we’ll probably want to do is explicitly choose the text encoding, since roxygen will otherwise complain at us:

cat("Encoding: UTF-8\n", file = "./BIOS7330/DESCRIPTION",append = TRUE)

Step 5: Properly document the one object we added

We’re going to use specially formatted comments which precede function definitions to automatically create documentation and configure the NAMESPACE file.

Add documentation to the matmult.R file following the pattern shown here:

http://r-pkgs.had.co.nz/man.html

Delete the existing NAMESPACE file and the “matmult.Rd” file in the “man” folder. We’re going to generate these automatically using devtools/roxygen.

Read and delete the “ReadAndDeleteMe” file.

file.remove("./BIOS7330/Read-and-delete-me")
file.remove("./BIOS7330/NAMESPACE")
file.remove("./BIOS7330/man/matmult.Rd")

Step 6: Regenerate the documentation:

library(devtools)
devtools::document("./BIOS7330")

Step 7: Build the package

devtools::build("./BIOS7330")

Step 8: Check the built package for problems

devtools::check("./BIOS7330")

The equivalent command line call is R CMD check BIOS7330

Step 9: Fix any errors/warnings/notes revealed with step 8.

Specifically: 1. (Warnings) The default BIOS7330-package.Rd file will have some misformattedsections and placeholder information. 2. (Note) The default Description field in the DESCRIPTION file will be too short. 3. (Note) The license in the DESCRIPTION file is non-standard.

Step 10: Install the package

devtools::install("./BIOS7330")

The equivalent command line procedure is R CMD INSTALL BIOS7330