R, and every package, provides help files for functions. The general syntax to search for help on any function, “function_name”, from a specific function that is in a package loaded into your namespace (your interactive R session):
?function_name
help(function_name)
This will load up a help page in RStudio (or as plain text in R by itself).
Each help page is broken down into sections:
Different functions might have different sections, but these are the main ones you should be aware of.
Tip: Running examples
From within the function help page, you can highlight code in the Examples and hit Ctrl+Return to run it in RStudio console. This gives you a quick way to get a feel for how a function works.
Tip: Reading help files
One of the most daunting aspects of R is the large number of functions available. It would be prohibitive, if not impossible, to remember the correct usage for every function you use. Luckily, the help files mean you don’t have to!
To seek help on special operators, use quotes:
?"<-"
Many packages come with “vignettes”: tutorials and extended example
documentation. Without any arguments, vignette() will list
all vignettes for all installed packages;
vignette(package="package-name") will list all available
vignettes for package-name, and
vignette("vignette-name") will open the specified
vignette.
If a package doesn’t have any vignettes, you can usually find help by
typing help("package-name").
Many packages also have a web presence. Google is your friend here. For example, here is the package website for the ggplot2 package: https://ggplot2.tidyverse.org/.
If you are looking for help on a function that is within a package, there are several ways to find it:
library(pkg_name))
and know the package name, you can simply use
?function_name?package_name::function_name or
help("function_name", package = "package_name")help(package = "package_name") and click on the link
for the function for which you are seeking helpIf you’re not sure what package a function is in or how it’s specifically spelled, you can do a fuzzy search:
??function_name
If you don’t know what function or package you need to use, CRAN Task Views is a specially maintained list of packages grouped into fields. This can be a good starting point.
Look at the help for the
sum()function. What are two ways you can pass numbers into the function so they are added together?Solution to exercise 1
?sum sum(1, 2, 3, 4, 5) x <- c(1, 2, 3) sum(x)
Look at the help for the
paste()function. You’ll need to use this later. What is the difference between thesepandcollapsearguments?Solution to exercise 2
To look at the help for the
paste()function, use:help("paste") ?pasteThe difference between
sepandcollapseis a little tricky. Thepastefunction accepts any number of arguments, each of which can be a vector of any length. Thesepargument specifies the string used between concatenated terms — by default, a space. The result is a vector as long as the longest argument supplied topaste. In contrast,collapsespecifies that after concatenation the elements are collapsed together using the given separator, the result being a single string. e.g.paste(c("a","b"), "c")[1] "a c" "b c"paste(c("a","b"), "c", sep = ",")[1] "a,c" "b,c"paste(c("a","b"), "c", collapse = "|")[1] "a c|b c"paste(c("a","b"), "c", sep = ",", collapse = "|")[1] "a,c|b,c"(For more information, scroll to the bottom of the
?pastehelp page and look at the examples, or tryexample('paste').)
Use help to find a function (and its associated parameters) that you could use to load data from a csv file in which columns are delimited with “ (tab) and the decimal point is a”.” (period). This check for decimal separator is important, especially if you are working with international colleagues, because different countries have different conventions for the decimal point (i.e. comma vs period). hint: use
??csvto lookup csv related functions.Solution to exercise 3
The standard R function for reading tab-delimited files with a period decimal separator is read.delim(). You can also do this with
read.table(file, sep="\t")(the period is the default decimal separator forread.table(), although you may have to change thecomment.charargument as well if your data file contains hash (#) characters.
Find the help for the
mutatefunction in thedplyrpackage. What is its purpose? What do you notice different about this help page?Solution to exercise 4
?dplyr::mutate # OR library(dplyr) ?mutate # OR help("mutate", package = "dplyr") # OR help(package = "dplyr") # and click on the 'mutate' link
mutate()adds new variables and preserves existing ones. The help page for mutate also documents the functiontransmute().