How to Get Help in R

How to get help in R?


Getting help on a function that you know the name of

Use ? or, equivalently, help.

?mean
help(mean) # same

For non-standard names use quotes or backquotes; see An Introduction to R: Getting help with functions and features:

For a feature specified by special characters, the argument must be enclosed in double or single quotes, making it a “character string”: This is also necessary for a few words with syntactic meaning including if, for and function."

?`if`
?"if" # same
help("if") # same

There are also help pages for datasets, general topics and some packages.

?iris
?Syntax
?lubridate

Use the example function to see examples of how to use it.

example(paste)
example(`for`)

The demo function gives longer demonstrations of how to use a function.

demo()                           # all demos in loaded pkgs
demo(package = .packages(all.available = TRUE)) # all demos
demo(plotmath)
demo(graphics)


Finding a function that you don't know the name of

Use ?? or, equivalently, help.search.

??regression
help.search("regression")

Again, non-standard names and phrases need to be quoted.

??"logistic regression"

apropos finds functions and variables in the current session-space (but not in installed but not-loaded packages) that match a regular expression.

apropos("z$") # all fns ending with "z"

rseek.org is an R search engine with a Firefox plugin.

RSiteSearch searches several sites directly from R.

findFn in sos wraps RSiteSearch returning the results as a HTML table.

RSiteSearch("logistic regression")

library(sos)
findFn("logistic regression")


Finding packages

available.packages tells you all the packages that are available in the repositories that you set via setRepositories. installed.packages tells you all the packages that you have installed in all the libraries specified in .libPaths. library (without any arguments) is similar, returning the names and tag-line of installed packages.

View(available.packages())
View(installed.packages())
library()
.libPaths()

Similarly, data with no arguments tells you which datasets are available on your machine.

data()

search tells you which packages have been loaded.

search()

packageDescription shows you the contents of a package's DESCRIPTION file. Likewise news read the NEWS file.

packageDescription("utils")    
news(package = "ggplot2")


Getting help on variables

ls lists the variables in an environment.

ls()                 # global environment
ls(all.names = TRUE) # including names beginning with '.'
ls("package:sp") # everything for the sp package

Most variables can be inspected using str or summary.

str(sleep)
summary(sleep)

ls.str is like a combination of ls and str.

ls.str()
ls.str("package:grDevices")
lsf.str("package:grDevices") # only functions

For large variables (particularly data frames), the head function is useful for displaying the first few rows.

head(sleep)

args shows you the arguments for a function.

args(read.csv)


General learning about R

The Info page is a very comprehensive set of links to free R resources.

Many topics in R are documented via vignettes, listed with browseVignettes.

browseVignettes()
vignette("intro_sp", package = "sp")

By combining vignette with edit, you can get its code chunks in an editor.

edit(vignette("intro_sp",package="sp"))    

Get Help in command line of R

For indexing functions, you can use :

help("[")

This works because [, for example, is in fact a function by itself, as you can see if you do :

x <- 1:5
do.call("[", list(x, 2))
[1] 2

Whereas ... is not a function, so help("...") won't work.

How to get function documentation in R Studio?

There is a help panel, probably on te right bottom corner. Press ctrl+3 to show it.

In R, can I get the help text for a function into a variable?

I think help.search could be of use. For instance, if I wanted everything in the base package:

x <- help.search("*", package="base")
entries <- data.frame(entry=x$matches$Entry, title=x$matches$Title)
entries[c(1, 100, 1000),]
# entry title
# 1 + Arithmetic Operators
# 100 c.POSIXlt Date-Time Classes
# 1000 encoding Functions to Manipulate Connections

get the documentation of an R function from the help as a string

You can extract help file as text:

helptext <- help(truehist, package=MASS)
tools:::Rd2txt(utils:::.getHelpFile(as.character(helptext)))

After you get the file you can easily find the arguments part, search for "data:" and retrieve the following text.

Redefining help_console function to get help on function from a given package

You have the wrong argument order and need to outsmart non-standard evaluation:

help_console2 <-
function (topic, pkg, format = c("text", "html", "latex", "Rd"), lines = NULL,
before = NULL, after = NULL)
{
format = match.arg(format)
if (!is.character(topic))
topic <- deparse(substitute(topic))
if (!is.character(pkg))
topic <- deparse(substitute(pkg))
helpfile = utils:::.getHelpFile(do.call(help, list(topic=topic, package=pkg)))
hs <- capture.output(switch(format, text = tools:::Rd2txt(helpfile),
html = tools:::Rd2HTML(helpfile), latex = tools:::Rd2latex(helpfile),
Rd = tools:::prepare_Rd(helpfile)))
if (!is.null(lines))
hs <- hs[lines]
hs <- c(before, hs, after)
cat(hs, sep = "\n")
invisible(hs)
}

help_console2(topic="lm", pkg="stats", format = "text", lines=1)
#Fitting Linear Models

Is there a command in R to view all the functions present in a package?

You can use lsf.str.

For instance:

lsf.str("package:dplyr")

To list all objects in the package use ls

ls("package:dplyr")

Note that the package must be attached.

To see the list of currently loaded packages use

search()

Alternatively calling the help would also do, even if the package is not attached:

help(package = dplyr)

Finally, you can use RStudio which provides an autocomplete function. So, for instance, typing dplyr:: in the console or while editing a file will result in a popup list of all dplyr functions/objects.

R - get help in browser instead of the in-build R helper


options(help_type = "html") # open help in browser
options(help_type = "text") # open help internally


Related Topics



Leave a reply



Submit