on.exit

on.exit(expr)
The on.exit function is used within other functions to specify a command to be run when exiting the parent function. For instance, if plotting parameters are automatically adjusted in a function, use on.exit to adjust plotting parameters back to their original settings.
  • expr – An R expression or command.

Continue reading

layout, par(mfrow)

Create a multi-paneled plotting window. The par(mfrow) function is handy for creating a simple multi-paneled plot, while layout should be used for customized panel plots of varying sizes.
par(mfrow)
  • mfrow – A vector of length 2, where the first argument specifies the number of rows and the second the number of columns of plots.
layout(mat, widths=rep(1, ncol(mat)), heights=rep(1, ncol(mat)))
  • mat – A matrix describing the panel layout, where the numbers describe the order in which to add the plots. A zero entry is interpreted as don’t plot anything here.
  • widths – The widths of the panel columns.
  • heights – The heights of the panel rows.

Continue reading

strsplit

strsplit(x, split, fixed=FALSE)
Split a character string or vector of character strings using a regular expression or a literal (fixed) string. The strsplit function outputs a list, where each list item corresponds to an element of x that has been split. In the simplest case, x is a single character string, and strsplit outputs a one-item list.
  • x – A character string or vector of character strings to split.
  • split – The character string to split x. If the split is an empty string (""), then x is split between every character.
  • fixed – If the split argument should be treated as fixed (i.e. literally). By default, the setting is FALSE, which means that split is treated like a regular expression.

Continue reading

grep

grep(pattern, x)
Search for a particular pattern in each element of a vector x. I remember the ordering of the arguments by remembering that the arguments follow the order of "needle in a haystack", where pattern is the needle and x is the haystack.
  • pattern – A regular expressions pattern, though a simple character string is probably sufficient for many people’s needs.
  • x – A character vector.

Continue reading

read.delim, write.table

The read.delim function is typically used to read in delimited text files, where data is organized in a data matrix with rows representing cases and columns representing variables. We can also write a matrix or data frame to a text file using the write.table function. Be sure to review the arguments of write.table carefully since the default settings clutter the text file (often unnecessarily).
read.delim(file, header=TRUE, sep="\t")
  • file – A file location.
  • header – Whether the first line describes the column names.
  • sep – The table delimiter, often times a tab (\t) or comma.
write.table(x, file="", …, quote=TRUE, sep=" ", row.names=TRUE)
  • x – A matrix or data frame to write to a file.
  • file – A file location.
  • quote – Whether characters or factors should have quotation marks written to the file.
  • sep – The table delimiter, often times a tab (\t) or comma.
  • row.names – Whether the row names of the matrix or data frame should be written as the first column in the file.

Continue reading

warning, stop

Use these functions to return a warning or error from another function. The warning function will output a message after a function finishes, while the stop function both stops the execution of the function and outputs an error message.
warning(char)
  • char – A character string that is output to the user.
stop(char)
  • char – A character string that is output to the user.

Continue reading