author: Lee Richardson date: 1/20/2016 font-family: 'Helvetica' autosize: true
Goals:
- Packages are the "fundamental units of shareable code"
- Organization, standardized tools
- Fulfillment, building something
"... from user to programmer to contributor, in the gradual progress that R encourages. Specifically: using R; simple programming; packages and methods; inter-system interfaces"
John Chambers (Software for Data Analysis: Programming with R)
Collection of files, organized in a specific way
When installed by R, can be used in R Sessions
Packages are installed into libraries: see yours using .libPaths()
Attached to the session using the library()
function.
library(lattice)
search()
R Packages, Chapter 1
R provides tools (R CMD INSTALL, R CMD BUILD, R CMD CHECK
, etc...) to move to different stages
"The goal of devtools is to make package development as painless as possible"
Hadley Wickham (R Packages)
install.packages("devtools")
library("devtools")
R Packages, chapter 1
An R (source) package is just files in a directory, formatted in a specific way.
To create the bare bones R package, just type:
devtools::create("/home/lee/leeR")
This should give us:
Devtools creates the "bare minimum" Description file (similar to package.skeleton()
)
This is fine for now, but becomes more important when you want to release your package
All of your R code goes into the R/
directory. Generally, this code is made up of functions
For example, lets create the file R/examples.R
and add the function:
leeR_demo <- function(string) {
print(string)
}
Now, we can load the package into memory:
devtools::load_all()
devtools::load_all()
loads your source package into memory
This is important because in developing a package, you often need to re-install the package over and over
Contrasting with the library()
function, which loads (then attaches) already installed packages
- R comes with tools for documentation (?sum
)
- You can utilize these tools by storing .Rd
files in a man/
directory. The .Rd
look a bit like LaTex
- R renders these files into html, pdf, or whichever format is needed.
The easiest way to document your R code is roxygen2
package.
Primarily, this allows you to combine code and documentation into a single file, and handles the .Rd
formatting (and NAMESPACE) for you
install.packages("roxygen2")
library(roxygen2)
#' Print a string!
#'
#' @param String character containing a string to
#' print
#' @return A printed string
#' @examples
#' leeR_demo()
leeR_demo <- function(string) {
print(string)
}
As a standard workflow, we can use:
devtools::document()
========================================================
This creates a man/
folder, and insert the corresponding man/leeR_demo.Rd
file that R uses to generate documentation.
Let's see what this looks like:
========================================================
\name{leeR_demo}
\alias{leeR_demo}
\title{Print a string!}
\usage{
leeR_demo(string)
}
\arguments{
\item{string}{character containing a string to
print}
}
\value{
A printed string
}
\description{
Print a string!
}
\examples{
leeR_demo()
}
?leeR_demo
Testing is critical to making sure your packages do what you expect.
The easiest way to get started with testing is the testthat
package, which also integrates with devtools
install.packages("testthat")
devtools::use_testthat()
This will set up the tests/testthat
directory where we can store tests
Test-that works hierarchically:
expect_equal(1, 1)
Test: Group of Expectations
File: File which contains tests. Must start with test
Next, let's create a file tests/testthat/test-example.R
context("Test our example")
test_that("our function works", {
expect_equal(leeR_demo("Hi!"), "Hi!")
expect_equal(2, 2)
expect_equal(1, 2)
})
Now run:
devtools::test()
This allows users to install your package without being hosted on CRAN.
For example, you could install the R package I just made using:
devtools::install_github(repo = "leerichardson/leeR")
library(leeR)
?leeR_demo
leeR_demo("Hi!")
Remarkably, the package is available to everyone with an internet connection
echo "# leeR" >> README.md
git init
git add --all
git commit -m "First Commit!"
git remote add origin https://github.com/username/packagename.git
git push -u origin master
Now check out the Github page, all of the code is there!
There are many more great features of to look into, including: - Including data - Vignettes (long form documentation, knitr) - Compiled code (C, C++, Fortran...)
Good Resources: - R Packages. Hadley Wickham - Software for Data Analysis: Programming with R. John Chambers - Writing R Extensions (the official documentation)
Questions?
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.