As with any programming language, you will inevitably run into problems, confusing situations, or just general questions when working in R
. Here are a few ways to start getting help
1. Read the error messages: If there is an issue with the way you have written or executed your code, R
will often print out an error message in your console (in red in RStudio). Do your best to decipher the message—read it carefully, and think about what is meant by each word in the message—or you can put that message directly into Google to search for more information.
2. Built-in documentation: RStudio provides built-in documentation. Functions and behaviors are all described in the same format, and often contain helpful examples. To search the documentation within R
(or in RStudio), type a question mark (?
) followed by the function name you’re using (e.g, ?sum
). You can perform a broader search of available documentation by typing two questions marks (??
) followed by your search term (e.g., ??sum
).
You can also look up help by using the help()
function (e.g., help(print)
will look up information on the print()
function, just as ?print
does). There is also an example()
function you can call to see examples of a function in action (e.g., example(print)
).
3. Packages: R
packages do not ship with the R
software by default, but rather need to be downloaded (once) and then loaded into your interpreter’s environment (each time you wish to use them). The base R
software provides install.packages()
function for installing packages, and the library()
function for loading them. The following example illustrates installing and loading the stringr
package:
# Install the `stringr` package. Only needs to be done once per computer install.packages("stringr") # Load the package (make `stringr` functions available in this `R` session) library("stringr") # quotes optional here, but best to include them