ZetCode

R help and documentation

last modified November 20, 2026

R ships with its documentation. Every function, data set, and package has a help page that states what it does, which arguments it accepts, and what it returns, and most pages end with runnable examples. The same installation also carries longer documents called vignettes and interactive scripts called demos. This article shows how to reach all of them from a script or a console: ?, help, args, example, apropos, help.search, vignette, and demo. The examples use R 4.5.2.

The help operator

A question mark followed by a name opens the help page for that name, so ?mean is the same request as help("mean"). Quotes are needed for names that are not syntactic, such as operators and punctuation: ?"+" and help("[") both work. In an interactive session the page opens in a pager or in the Help pane of RStudio, and help.start() opens a browser with an index of the installed documentation.

help also returns an object. When the result is assigned to a name instead of being displayed, no page opens, and the object can be inspected. Its class is help_files_with_topic, the topic attribute stores the name that was found, and its length is the number of topics that matched the request.

help-object.R
h <- help("mean")
cat("class:", class(h), "\n")
cat("topic:", attr(h, "topic"), "\n")
cat("matches:", length(h), "\n")

The single match is the page for mean. The topic attribute repeats the name, and the object also stores the paths of the files that hold the page. The class, the topic, and the length are enough to confirm that the request found one topic. A typo returns the same class with length 0, and R prints a message that suggests trying the double question mark instead. That suggestion is often faster than correcting the spelling by hand.

$ Rscript help-object.R
class: help_files_with_topic 
topic: mean 
matches: 1 

Function arguments

args prints the argument list of a function without opening the help page. It shows the names, the order, and the default values, which is usually enough to call a function correctly. The next script asks for the arguments of mean and of lm.

arguments.R
args(mean)
args(lm)

mean is a generic function with one required argument and ..., and the concrete method for numeric vectors has the trim and na.rm arguments shown in the Usage section of its page. lm has a longer list, with defaults such as method = "qr". The word NULL after each signature is the value that args returns after printing; it is not part of the signature.

$ Rscript arguments.R
function (x, ...) 
NULL
function (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...) 
NULL

Running the examples

example runs the examples at the end of a help page. It prints each expression with the topic name as a prompt, evaluates it, and prints the result, so the output is a small annotated transcript. This is the fastest way to see a function in use with realistic values.

examples.R
example(mean)

The example builds x from the numbers 0 to 10 and the value 50, takes the mean, and then takes a trimmed mean that drops ten percent of the observations from each end. The trim removes 0 and 50, so the second result is the mean of 1 to 10. With echo = FALSE the expressions are hidden and only the results are printed; with ask = TRUE R waits for a key press between plots.

$ Rscript examples.R

mean> x <- c(0:10, 50)

mean> xm <- mean(x)

mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50

Searching by name

apropos searches the names of the objects in the loaded packages and returns every name that matches a pattern. It is a name search, not a text search: it finds functions, data sets, and variables whose name contains the pattern. The result below is the complete list for mean in a fresh session.

lookup.R
hits <- apropos("mean")
cat("matches:", hits, "\n")

The list includes the generic mean, the methods mean.Date and mean.default, the column and row helpers, and weighted.mean. The pattern is a regular expression, so it can be anchored or restricted; the regular expressions article explains that syntax. Passing mode = "function" removes data sets and other objects from the result.

$ Rscript lookup.R
matches: .colMeans .rowMeans colMeans kmeans mean mean.Date mean.default mean.difftime mean.POSIXct mean.POSIXlt rowMeans weighted.mean 

Searching the documentation

help.search searches the names, titles, aliases, concepts, and keyword fields of the help pages, so it finds a topic even when its name is unknown. The shorthand is a double question mark, ??regression. The result is a packageIQR object whose matches element is a table with one row per page; nrow returns the number of rows and names returns the column names.

help-search.R
hs <- help.search("regression", package = "stats")
cat("rows:", nrow(hs$matches), "\n")
cat("columns:", names(hs$matches), "\n")

Restricting the search with package = "stats" keeps the count small. Without the restriction the same search also looks in every installed package, which is why the full result can be much longer. The columns identify the page, its package, and the library directory that holds it. For a search across the whole CRAN archive and the mailing lists, RSiteSearch("regression") opens a web page instead.

$ Rscript help-search.R
rows: 73 
columns: Topic Title Name ID Package LibPath Type Field Entry 

Vignettes

A vignette is a longer document that comes with a package and explains a whole workflow rather than one function. Vignettes are built when the package is installed and are usually shipped as PDF or HTML files. vignette(package = "Matrix") lists the vignettes of one package, and browseVignettes() opens an index of all installed vignettes in a browser.

vignettes.R
v <- vignette(package = "Matrix")
cat("Matrix vignettes:", nrow(v$results), "\n")
cat("columns:", colnames(v$results), "\n")

The Matrix package ships five vignettes, and the results table has one row for each. The Item column holds the short names such as Intro2Matrix and sparseModels, while Package and LibPath locate the files. A vignette is read with a PDF or HTML viewer; the table only tells you what is available.

$ Rscript vignettes.R
Matrix vignettes: 5 
columns: Package LibPath Item Title 

Demos

A demo is a script that runs a guided example, often with plots, and is meant to be watched rather than read. demo(package = "graphics") lists the demos that belong to a package, and demo("persp") runs one of them. The return value has the same shape as the vignette list.

demos.R
demos <- demo(package = "graphics")
cat("graphics demos:", nrow(demos$results), "\n")
cat("columns:", colnames(demos$results), "\n")

The graphics package has six demos, including graphics, image, persp, and plotmath. The Item column holds those names. A demo may open a graphics device and wait for input, so it is more useful in an interactive session than in a batch script.

$ Rscript demos.R
graphics demos: 6 
columns: Package LibPath Item Title 

A complete example: exploring median

The final script uses the help tools on an unfamiliar function. It confirms that the topic exists, prints the argument list, runs the examples, and asks which related names are loaded. This is the sequence to follow when a function appears in someone else's code and its behavior is not obvious.

explore.R
h <- help("median")
cat("matches:", length(h), "\n")
args(median)
example(median)
cat("related:", apropos("median"), "\n")

The page for median exists, the signature has x, na.rm, and ..., and the two examples show the even-length and odd-length cases. The last line lists median and its default method, which confirms that the generic dispatches to a method for numeric vectors. From here the next step is to read the Description and Value sections of the page itself.

$ Rscript explore.R
matches: 1 
function (x, na.rm = FALSE, ...) 
NULL

median> median(1:4)                # = 2.5 [even number]
[1] 2.5

median> median(c(1:3, 100, 1000))  # = 3 [odd, robust]
[1] 3
related: median median.default 

Next steps

The functions article explains the argument matching that args displays, including ... and default values. The packages article shows how to install a package and find its documentation, and the debugging article uses the same pages when a call fails. The console and scripts article covers the command line and the startup files that decide which documentation is available.

Source

R manual: help

R manual: args

R manual: example

R manual: apropos

R manual: help.search

R manual: vignette

R manual: demo

In this article we used the documentation that comes with every R installation. The help operator returned an object of class help_files_with_topic, args printed the signatures of mean and lm, and example ran the annotated examples for mean. apropos searched object names, help.search searched page titles and keywords, and the vignette and demo lists showed the longer documents and guided scripts in a package. The final script applied the same steps to median. The functions article explains the argument lists that these tools display.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all R Language tutorials.