Chapter 3 Seeking Help in R
3.1 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.
3.1 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.
3.1 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!
3.3 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")
.
3.4 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:
3.5 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.
3.5 Challenge 1
Look at the help for the
sum
function. What are two ways you can pass numbers into the function so the are added together:
3.5 Challenge 2
Look at the help for the
paste
function. You’ll need to use this later. What is the difference between thesep
andcollapse
arguments?3.5 Solution to Challenge 2
To look at the help for the
paste()
function, use:The difference between
sep
andcollapse
is a little tricky. Thepaste
function accepts any number of arguments, each of which can be a vector of any length. Thesep
argument specifies the string used between concatenated terms — by default, a space. The result is a vector as long as the longest argument supplied topaste
. In contrast,collapse
specifies that after concatenation the elements are collapsed together using the given separator, the result being a single string. e.g.[1] "a c" "b c"
[1] "a,c" "b,c"
[1] "a c|b c"
[1] "a,c|b,c"
(For more information, scroll to the bottom of the
?paste
help page and look at the examples, or tryexample('paste')
.)
3.5 Challenge 3
Use help to find a function (and its associated parameters) that you could use to load data from a csv file in which columns are delimited with “ (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
??csv
to lookup csv related functions.3.5 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 forread.table()
, although you may have to change thecomment.char
argument as well if your data file contains hash (#) characters