- Part A
- R-packages – an introduction (10 minutes)
- Structure and contents of R-packages (30 minutes)
- From scripts to functions (20 minutes)
- Part B
- Building a template package the prospective way (10 minutes)
- Maintaining a package (20 minutes)
January 18, 2017
By the way, Hadley Wickhams Advanced R is a warm recommendation.
devtools
by Hadley Wickhamryoxygen2
by Hadley WickhamPackages comprise the full set of self-contained components
Libraries are no packages! Libraries host packages. You pull a package from the library.
The code is automatically tested (by your examples and other routines)
Working with packages simply saves time and brain cells
A working example (you better don't trust the documentation)
A set of further stuff that will be covered later
Title: Environmental seismology toolbox Description: A collection of functions to handle seismic data for the purpose of investigating the seismic signals emitted by Earth surface processes. The package supports inporting standard formats, data preparation and analysis techniques and data visualisation.
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
citation("PACKAGENAME", auto = TRUE)
The key element to inform who can use the package for which purpose!
License: file LICENSE
) or a keyword of standard licenses (read more):
They are more than just counters, they define dependency satisfactions
Make use of a NEWS file to announce version history and changes.
Omitting it means, nobody will be able to use your package
Additional documentation is covered by vignettes (not covered here)
In R, documentation in *.Rd
-files is highly formalised and follows a LaTex scheme (short version, long version)
roxygen2
and Rd2roxygen
inlinedocs
(no longer updated)*.rda
files (generated with save()
)data
src
will be compiled during installationinst
will be copied to main directorya <- 10:50 print(a) plot(a) b <- 210:250 plot(a, b) A <- a * b c <- 5 V <- A * c print(A) print(V) plot(a,V)
a <- 10:50 b <- 210:250 c <- 5 A <- a * b V <- A * c plot(a) plot(a, b) plot(a,V) print(a) print(A) print(V)
## define object geometry a <- 10:50 b <- 210:250 c <- 5 ## calculate area and volume A <- a * b V <- A * c ## plot object dimensions plot(a) plot(a, b) plot(a, V) ## print values print(a) print(A) print(V)
f <- function(a, b, c) { ## calculate area and volume A <- a * b V <- A * c ## plot object dimensions plot(a) plot(a, b) plot(a, V) ## return values return(list(A = A, V = V)) }
f <- function(a, b, c, plot = TRUE) { ## calculate area and volume A <- a * b V <- A * c ## optionally plot object dimensions if(plot == TRUE) { plot(a) plot(a, b) plot(a, V) } ## return values return(list(A = A, V = V)) }
f(a = 10, b = 100, c = 5, plot = FALSE)
## $A ## [1] 1000 ## ## $V ## [1] 5000
Ooops, what did we forget?
Function documentation (in a separate file)
\name{f} \alias{f} \title{Calculate and plot cuboid areas and volumes.} \usage{f(a, b, c, plot = TRUE)} \arguments{ \item{a}{\code{Numeric} vector, length of the cuboid.} \item{b}{\code{Numeric} vector, width of the cuboid.} \item{c}{\code{Numeric} vector, height of the cuboid.} } \value{A list with cuboid area and volume.} \description{The function takes numeric vectors of the cardinal dimensions of a cuboid object and calculates area and volume. The results can optionally be plotted.} \examples{f(a = 10, b = 100, c = 5, plot = FALSE)} \author{Michael Dietze}
Another brief function example
f <- function(x, p = 2) { ## calculate the power of x y <- x^p ## return value return(y) }
Can be rewritten like this:
#' Calculate the power of a vector # TITLE #' #' The function calculates the power p of a vector x. # DESCRIPTION #' #' The function simply combines the arguments. # DETAILS #' #' @param x input vector # ARGUMENTS #' @param p power exponent # ARGUMENTS #' @return vector of the power p of x. # VALUE #' @author Michael Dietze # AUTHOR(S) #' @examples #' f(x = 10, p = 3) # EXAMPLES #' @export f # NAMESPACE ENTRY f <- function(x, p = 2) { # USAGE return(x^p) }
And become someting like:
roxygen2 is a package that parses function source files for tags and converts them to the LaTex-like structure of a *.Rd
-file.
Third and further set of lines becomes details (optional)
Further down follow tagged items
@param
- Function arguments, note argument and then description@return
- Function value@examples
- Examples section@export
- Namespace export, usually the function name@seealso
- Related functions to link to@keywords
- Well, keywords@section
- Arbitrary sections to further structure the documentation\emph{}
, \strong{}
, \code{}
)\code{\link{}}
, \href{}{}
)\enumerate{}
, \itemize{}
)\eqn{}
, \deqn{}
)\tabular{}{\tab \cr}
)Luminescence::analyse_baSAR.R
@format
- Overview of data structure (copy-paste output of str()
)@source
- Source of the data set, e.g., the internet linkR/DATA_SET.R
#' Ten numbers from 1 to 10 #' #' A dataset containing ten ordered natural numbers #' #' @format A vector with 10 variables: #' int [1:10] 1 2 3 4 5 6 7 8 9 10 "x"
NULL
-objectPACKAGE_NAME-package.R
in R/
#' A package of diverse functions #' #' The package is used to store all my functions, save from my brain. #' #' @docType package #' @name PACKAGE_NAME #' @import stats #' @importFrom utils read.table, write.table NULL