Getting help in R
Overview
Teaching: 10 min
Exercises: 10 minQuestions
How can I get help in R?
Objectives
To be able read R help files for functions and special operators.
To be able to use CRAN task views to identify packages to solve a problem.
To be able to seek help from your peers.
Reading Help files
R, and every package, provide help files for functions. The general syntax to search for help on functions is:
?function_name
help(function_name)
This will load up a help page in RStudio. It will work as long as you have loaded the package that the function belongs to (i.e., the function is in your ‘namespace’).
Each help page is broken down into sections:
- Description: An extended description of what the function does.
- Usage: The arguments of the function and their default values.
- Arguments: An explanation of the data each argument is expecting.
- Details: Any important details to be aware of.
- Value: The data the function returns.
- See Also: Any related functions you might find useful.
- Examples: Some examples for how to use the function.
Different functions might have different sections, but these are the main ones you should be aware of.
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!
Special Operators
To seek help on special operators, use quotes:
?"<-"
Getting help on packages
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")
.
When you kind of remember the function
If you’re not sure what package a function is in, or how it’s specifically spelled you can do a fuzzy search:
??function_name
When you have no idea where to begin
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.
When your code doesn’t work: seeking help from your peers
If you’re having trouble using a function, 9 times out of 10,
the answers you are seeking have already been answered on
Stack Overflow. You can search using
the [r]
tag.
If you can’t find the answer, there are a few useful functions to help you ask a question from your peers:
?dput
Will dump the data you’re working with into a format so that it can be copy and pasted by anyone else into their R session.
sessionInfo()
will print out your current version of R, as well as any packages you
have loaded. This can be useful for others to help reproduce and debug
your issue.
sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.40 requirements_0.0.0.9000 remotes_2.4.2.1
loaded via a namespace (and not attached):
[1] digest_0.6.30 lifecycle_1.0.4 magrittr_2.0.3 evaluate_0.18
[5] rlang_1.1.3 stringi_1.7.8 cli_3.6.2 vctrs_0.6.5
[9] rmarkdown_2.18 tools_4.2.2 stringr_1.5.1 glue_1.6.2
[13] xfun_0.34 yaml_2.3.6 fastmap_1.1.1 compiler_4.2.2
[17] htmltools_0.5.7
Challenge 1
Look at the help for the
log
function. What is the default base used to calculate logarithms?What is the name of the argument used to change the base?
Solution to Challenge 1
The
log()
function calculate natural logarithms by default. To change the base, you can set thebase
argument.
Challenge 2
Look at the help for the
sum
function. It has thena.rm
agrument to control how it deals with missing values.Run the final two code examples at the bottom of the help file. What result does each give?
Solution to Challenge 2
sum(1:5, NA)
[1] NA
sum(1:5, NA, na.rm = TRUE)
[1] 15
The first line gives a result of
NA
. But whenna.rm = TRUE
in the second,sum
first removes theNA
value and then calculates the sum of 1, 2, 3, 4, & 5 giving 15.
Other ports of call
Key Points
Use
help()
to get online help in R.