Getting Help

A plethora of resources are available to help you learn more about R. These include several facilities within R itself and, of course, on the Web.

Much work has gone into making R self-documenting. We’ll look at some of R’s built-in help facilities and then at those available on the Internet.

To get online help, invoke help(). For example, to get information on the seq() function, type this:

> help(seq)

The shortcut to help() is a question mark (?):

> ?seq

Special characters and some reserved words must be quoted when used with the help() function. For instance, you need to type the following to get help on the < operator:

> ?"<"

And to see what the online manual has to say about for loops, enter this:

> ?"for"

Each of the help entries comes with examples. One really nice feature of R is that the example() function will actually run those examples for you. Here’s an illustration:

> example(seq)

seq> seq(0, 1, length.out=11)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

seq> seq(stats::rnorm(20))
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

seq> seq(1, 9, by = 2) # match
[1] 1 3 5 7 9

seq> seq(1, 9, by = pi)# stay below
[1] 1.000000 4.141593 7.283185

seq> seq(1, 6, by = 3)
[1] 1 4

seq> seq(1.575, 5.125, by=0.05)
 [1] 1.575 1.625 1.675 1.725 1.775 1.825 1.875 1.925 1.975 2.025 2.075 2.125
[13] 2.175 2.225 2.275 2.325 2.375 2.425 2.475 2.525 2.575 2.625 2.675 2.725
[25] 2.775 2.825 2.875 2.925 2.975 3.025 3.075 3.125 3.175 3.225 3.275 3.325
[37] 3.375 3.425 3.475 3.525 3.575 3.625 3.675 3.725 3.775 3.825 3.875 3.925
[49] 3.975 4.025 4.075 4.125 4.175 4.225 4.275 4.325 4.375 4.425 4.475 4.525
[61] 4.575 4.625 4.675 4.725 4.775 4.825 4.875 4.925 4.975 5.025 5.075 5.125

seq> seq(17) # same as 1:17
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17

The seq() function generates various kinds of numeric sequences in arithmetic progression. Running example(seq) resulted in R’s running some examples of seq() before our very eyes.

Imagine how useful this can be for graphics! If you are interested in seeing what one of R’s excellent graphics functions does, the example() function will give you a “graphic” illustration.

To see a quick and very nice example, try running the following command:

> example(persp)

This displays a series of sample graphs for the persp() function. One of these is shown in Figure 1-2. Press enter in the R console when you are ready to go to the next one. Note that the code for each example is shown in the console, so you can experiment by tweaking the arguments.

You can use the function help.search() to do a Google-style search through R’s documentation. For instance, say you need a function to generate random variates from multivariate normal distributions. To determine which function, if any, does this, you could try something like this:

> help.search("multivariate normal")

This produces a response containing this excerpt:

mvrnorm(MASS)           Simulate from a Multivariate Normal
                        Distribution

You can see that the function mvrnorm() will do the job, and it is in the package MASS.

There is also a question-mark shortcut to help.search():

> ??"multivariate normal"

R’s internal help files include more than just pages for specific functions. For example, the previous section mentioned that the function mvrnorm() is in the package MASS. You can get information about the function by entering this:

> ?mvrnorm

And you can also learn about the entire package by typing this:

> help(package=MASS)

Help is available for general topics, too. For instance, if you’re interested in learning about files, type the following:

> ?files

This gives you information about a number of file-manipulation functions, such as file.create().

Here are some other topics:

Arithmetic
Comparison
Control
Dates
Extract
Math
Memory
NA
NULL
NumericaConstants
Paren
Quotes
Startup
Syntax

You may find it helpful to browse through these topics, even without a specific goal in mind.

Recall that R has batch commands that allow you to run a command directly from your operating system’s shell. To get help on a particular batch command, you can type:

R CMD command --help

For example, to learn all the options associated with the INSTALL command (discussed in Appendix B), you can type this:

R CMD INSTALL --help

There are many excellent resources on R on the Internet. Here are a few:

Because of its single-letter name, R is difficult to search for using general-purpose search engines such as Google. But there are tricks you can employ. One approach is to use Google’s filetype criterion. To search for R scripts (files having a .R suffix) pertaining to, say, permutations, enter this:

filetype:R permutations -rebol

The -rebol asks Google to exclude pages with the word “rebol,” as the REBOL programming language uses the same suffix.

The Comprehensive R Archive Network (CRAN), at http://cran.r-project.org/, is a repository of user-contributed R code and thus makes for a good Google search term. Searching for “lm CRAN,” for instance, will help you find material on R’s lm() function.