OverviewTeaching: 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 any function, “function_name”, from a specific function that is in a package loaded into your namespace (your interactive R session):
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:
- 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: 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 is 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:
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
vignette("vignette-name") will open the specified vignette.
If a package doesn’t have any vignettes, you can usually find help by typing
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:
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
If you can’t find the answer, there are a few useful functions to help you ask a question from your peers:
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.
R version 3.6.3 (2017-01-27) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 14.04.5 LTS Matrix products: default BLAS: /home/travis/R-bin/lib/R/lib/libRblas.so LAPACK: /home/travis/R-bin/lib/R/lib/libRlapack.so locale:  LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C  LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8  LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8  LC_PAPER=en_US.UTF-8 LC_NAME=C  LC_ADDRESS=C LC_TELEPHONE=C  LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages:  stats graphics grDevices utils datasets methods base other attached packages:  knitr_1.29 requirements_0.0.0.9000 remotes_2.1.1 loaded via a namespace (and not attached):  compiler_3.6.3 magrittr_1.5 htmltools_0.5.0 tools_3.6.3  yaml_2.2.1 stringi_1.4.6 rmarkdown_2.3 stringr_1.4.0  xfun_0.15 digest_0.6.25 rlang_0.4.6 evaluate_0.14
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.
Look at the help for the
cfunction. What kind of vector do you expect you will create if you evaluate the following:
c(1, 2, 3) c('d', 'e', 'f') c(1, 2, 'f')
Solution to Challenge 1
c()function creates a vector, in which all elements are the same type. In the first case, the elements are numeric, in the second, they are characters, and in the third they are characters: the numeric values are “coerced” to be characters.
Look at the help for the
pastefunction. You’ll need to use this later. What is the difference between the
Solution to Challenge 2
To look at the help for the
The difference between
collapseis a little tricky. The
pastefunction accepts any number of arguments, each of which can be a vector of any length. The
separgument specifies the string used between concatenated terms — by default, a space. The result is a vector as long as the longest argument supplied to
paste. In contrast,
collapsespecifies that after concatenation the elements are collapsed together using the given separator, the result being a single string. e.g.
 "a c" "b c"
paste(c("a","b"), "c", sep = ",")
 "a,c" "b,c"
paste(c("a","b"), "c", collapse = "|")
 "a c|b c"
paste(c("a","b"), "c", sep = ",", collapse = "|")
(For more information, scroll to the bottom of the
?pastehelp page and look at the examples, or try
Use help to find a function (and its associated parameters) that you could use to load data from a tabular file in which columns are delimited with “\t” (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
??"read table"to look up functions related to reading in tabular data.
Solution to Challenge 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 for
read.table(), although you may have to change the
comment.charargument as well if your data file contains hash (#) characters
Other ports of call
help()to get online help in R.