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.
R, and every package, provide help files for functions. To search for help on a function from a specific function that is in a package loaded into your namespace (your interactive R session):
help(function_name)
?function_name
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:
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!
To seek help on special operators, use quotes:
?"+"
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")
.
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
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.
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.
Try these challenges to practice finding help in R.
Challenge 1
Look at the help for the
c
function. Try the following commands and see if you can use the help file to explain the output you see:c(1, 2, 3) c('d', 'e', 'f') c(1, 5, 'g')
Solution to Challenge 1
c(1,2,3) [1] 1 2 3 c('d','e','f') [1] "d" "e" "f" c(1,5,'g') [1] "1" "5" "g"
The
c
function creates a vector, which is a sequence of individual elements in R. In a vector, all elements must be the same data type. In the first case, we created a numeric vector. For the second, our vector is a character vector which you can see by the quotes that R put around each element. In the third example, since we gave R both numeric and character elements, R converted all the elements to characters so that our vector contained all the same type of elements.Don’t worry if this is confusing right now. We will clarify more in our next lesson about data structures in R.
Challenge 2
Look at the help for the
typeof
function. What does this function do? Try creating the following objects and using this function on them.
- m <- 15
- n <- “Lincoln”
Explain to your neighbor what this function is telling us about these objects:
Solution to Challenge 2
help("typeof") ?typeof
We can see from the help file that
typeof
tells us the type of an object in R. We will discuss R data types further in the next lecture.m <- 15 typeof(m)
[1] "double"
This is telling us that
m
is the data typedouble
which is a numeric data type in R.n <- "Lincoln" typeof(n)
[1] "character"
Here,
n
is the data typecharacter
which is the data type R uses for strings.
Challenge 3 - Advanced
The
read.table
function is used to read data from external files into the R environment. For example: to read in a file called “data.csv” you would use the following command:read.table(file = "data.csv")
Look at the help for the
read.table
function. In particular, look at theArguments
section. These are additional options we can put in our command to customize its action.What argument would you use if you wanted to read in a file without a header? What argument would allow you to switch between a comma separated file and a tab separated file?
Solution to Challenge 3
To view the help for the
read.table
function, you can type one of the following commands into your console:help("read.table") ?read.table
By looking at the arguments section of the help file, we can see that to prevent R from automatically assuming the first row of your file is a header row, you would specify
header = FALSE
.read.table(file = "data.csv", header = FALSE)
To switch between comma separated files (csv) and tab separated files (tsv), you would use the
sep
argument.
Key Points
Use
help
to get online help in R.