Help in R
In this tutorial you will learn how to get help in R, explore all the available resources and obtain the answers you need when coding in R language. So if you are new to R or you want to know all the available help you can find, keep reading!
Free R books and manuals
There are free available books online in case you need a book to help you to learn R without spending money. The official resources can be found on the web page of the R-project.
https://cran.r-project.org/manuals.html
Book | Level |
---|---|
An Introduction to R | Beginner and intermediate |
R Data Import/Export | Beginner and intermediate |
R Installation and Administration | Advanced |
Writing R Extensions | Advanced |
R language definition (Draft) | Advanced |
R Internals | Advanced |
The R Reference Index | Advanced |
In addition, if you are working with R GUI (not RStudio), you can go to menu Help → Manuals (PDF) and access to several books.
There are also contributed and translated resources, but the page is no longer actively maintained. You can see the full list in the following link.
https://cran.r-project.org/other-docs.html
Package PDF manuals
It should be noted that there are PDF manuals of every R package. Some of them are more extensive and detailed than others, as there is no restriction about the quality of the documentation when uploading a package to CRAN. You can access the PDF documentation with links of the following structure.
https://cran.r-project.org/web/packages/Package_Name/Package_Name.pdf
Help.start function
If you are new to R programming language and you feel lost, you can call the help.start
function.
help.start() # HTML help will open
Once executed, if you are using RStudio the Help
tab will show you HTML help. Otherwise, a browser will be open with the HTML documentation. Then, you will be able to access to manuals, references and miscellaneous or specific material.
R Help function
Help of a function
R has documentation integrated in the IDE. In order to obtain help of any function you can call the help
function or, equivalently, the ?
function.
# Obtaining help of the rnorm function
help(rnorm)
?rnorm # Equivalent
The help will be in HTML format and will contain at least the description of the function, the arguments with its definition and an example.
Help of a package
Equivalently, you can obtain package help in R if you specify the package in the package
argument.
help(package = "stats")
?stats # Equivalent
Help.search function
Sometimes you don’t know the exact name of the function from which you want to get the documentation. In these cases the help.search
function looks for documentation of some word in all packages, even if they are not loaded in memory. As an example, if you don’t know which function to use for calculating the median of a data set, you can use the following code:
help.search("median")
In addition, you can also specify the package where you want to look for the term with the package
argument.
help.search("median", package = "stats")
If you want to obtain more information about the arguments of the help.search
function, recall to use ?help.search
.
Equivalently, you can use the ??
function.
??median
Note that you can obtain the full list of functions of a package, among other information, running library(help = “package_name”)
Apropos function
When looking for a specific topic, or when you only remember part of the name of a function, you can search for that string with the apropos
function. As an example, in case you want to look for functions related to plot
, you can obtain a list of all the functions that contain ‘plot’ within its name writing:
# Functions that contain 'plot'
apropos("plot", mode = "function")
"assocplot" "barplot" "barplot.default"
"biplot" "boxplot" "boxplot.default"
You could also use a regular expression. As an example, if you want to display all functions that starts with ‘plot’ you can use:
# Functions that starts with 'plot'
apropos("^plot", mode = "function")
"plot" "plot.default" "plot.design"
"plot.ecdf" "plot.function" "plot.new"
RSiteSearch function
The RSiteSearch
function is used for looking for online help of any topic. As an example, if you search for ‘plot’ again, with this function a web page will open and will show results of your query.
# Searching for the string 'plot' in online CRAN resources
RSiteSearch("plot")
R cheat sheets
R cheat sheets are quick guides of some topic, like ggplot2 or regular expressions, among others. You can download them in PDF from the RStudio official web page:
https://cran.r-project.org/manuals.html
Datasets help
If you want to see the full list of available datasets, you can use the data
function as follows:
In case you prefer to show the full list of datasets of all packages you have installed you can call the function and specify the package
argument:
data(package = .packages(all.available = TRUE))
Note that you can set any packages you want in that argument, instead of calling all with the .packages
function.
Moreover, when working with datasets you also need information about them, such information about the variables or how the data was collected. For that purpose just call the help
or ?
functions, specifying properly the name of the dataset. As an example, if you want information about the diamonds
dataset, type one of the following lines:
help("diamonds")
?diamonds # Equivalent
Function examples
Packages include examples of use of their own functions, that you can call with the example
function. For instance, you can see different plot
examples typing:
example("plot")
After executing, several plots will be displayed by order. You can see the next plot pressing Enter or clicking over the plot (in base R). In case you want to leave, press Esc. Other example can be the one displayed in the next code block.
example("quantile")
example(quantile) # Equivalent
The previous code will output an example of use of the quantile
function with random sample data.
If you want to execute a function example of a package you first need to load the package.
R package demos
Likewise the example
function, there exists other function named demo
that calls more extensive examples of some relevant functions.
Note that only a few packages include demos.
If you call the demo
function it will output a list of available demos to execute.
demo()
Once you decided which one you want to execute, just type it inside the function with or without quotation marks.
demo("smooth")
demo(smooth) # Equivalent
R vignettes
The vignettes are long guides of packages that describe its use with wide explanations and more detailed examples than the ones in the base documentation, written in Rmarkdown format. Note that many packages contain vignettes, but not all. You can see the full list of vignettes of your installed R packages with the vignette
function.
vignette()
You can also specify one or more packages instead of looking for all vignettes with the package
argument.
vignette(package = "ggplot2")
Vignettes in package ‘ggplot2’:
ggplot2-specs Aesthetic specifications (source, html)
extending-ggplot2 Extending ggplot2 (source, html)
ggplot2-in-packages Using ggplot2 in packages (source, html)
Once you decided which to see, type:
vignette("name_of_the_vignette")
# Example:
vignette("ggplot2-specs")
After calling the function, a window will open with the full documentation.