2.7 Getting help with packages and functions
How do you learn to use a package or know what functions it contains? A well-written R package includes a list of all its available functions as well as help files for every function. For a package’s main help file, type help(package="NameOfPackage")
. You can try it now with dplyr: help(package="dplyr")
. You should see the main dplyr help file appear in RStudio’s lower-right pane. There’s a list of available functions, and you can click through each one to get more details about the function.
Some packages also have what are called vignettes, which are short articles explaining how to use the package or do specific tasks. If you don’t see a link to “User guides, package vignettes and other documentation” on the main help page, you can try browseVignettes("NameOfPackage")
. The command browseVignettes()
without anything inside the parentheses will show you all available vignettes on your system. Note: The more technical term for what’s between the parentheses when you run a function is argument.
For dplyr, run help(package = "dplyr")
and click the link to User guides, package vignettes, and other documentation in the main help page. Select the dplyr::dplyr vignette (dplyr::introduction in some earlier versions of the package). There, you’ll see a nice overview of the package as well as how to use several of its most important functions.
To see a function’s help file without first loading the package’s main help file, you can type help("NameOfFunction")
or just ?NameOfFunction
(remember that function names are case sensitive). The package needs to be loaded into your session with library("NameOfPackage")
for the ?
shortcut to work. If the package isn’t loaded, help(NameOfFunction, package = "NameOfPackage")
will work.
In addition, if you’ve typed the name of a function (either in the console or script pane) from a package that’s loaded into your working session (or from base R), just hit the F1 key. Your cursor has to be just after the last letter of the function name for this to work, so if you’ve already typed, say, arrange(), move your cursor to just after the e in arrange and F1 help should still work; if your cursor is in the middle of the parentheses, it won’t. I was using RStudio for several years before I discovered this time-saver and stopped jumping from my script window to the console to type ?NameOfFunction
to see a function’s help file.
Finally, if you want to search for a function but either aren’t sure which package it’s in or don’t want to type out the lengthy help(NameOfFunction, package = "NameOfPackage")
when its library isn’t loaded, you can use the double question mark. ??median
will bring up links to all functions that contain median in either their name or description for all packages on your system, whether or not they’re currently loaded into memory. This will also do a little partial matching, bringing up results that almost match your search term.
When you’re ready to close RStudio, you’ll be asked if you want to save any unsaved files – you probably do, even though RStudio will keep your project’s unsaved files as is – and whether you want to save your workspace image to an .RData file. In general, I’d advise not to save your workspace, because all of your variables will be stored and re-loaded the next time you launch RStudio. It’s too easy to forget about previously stored variables that can interfere with later work, not to mention taking up memory with things you might not need. Best to start off clean each session. If you’re performing lengthy calculations that you don’t want to repeat, there are ways to save variables to disk without auto-loading your entire workspace.
If, like I am, you’re sure you don’t want to be saving your workspace, you can tell RStudio to stop asking you. Go to Tools > Global Options and choose Never from the drop-down list for Save workspace to .RData on exit
.