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.
install.packages(c("devtools", "roxygen2", "testthat", "knitr"))
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
}
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).
# 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)
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")
library(devtools)
devtools::document("./BIOS7330")
devtools::build("./BIOS7330")
devtools::check("./BIOS7330")
The equivalent command line call is R CMD check BIOS7330
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.
devtools::install("./BIOS7330")
The equivalent command line procedure is R CMD INSTALL BIOS7330