Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get help in R?

Tags:

r

r-faq

What is the possible documentation available for R package? For example I try to understand sp package.

In addition to help(sp), what are the other functions for searching through help and documentation?

like image 649
Tomas Greif Avatar asked Mar 08 '13 08:03

Tomas Greif


4 Answers

Getting help on a function that you know the name of

Use ? or, equivalently, help.

?mean
help(mean) # same

For non-standard names use quotes or backquotes; see An Introduction to R: Getting help with functions and features:

For a feature specified by special characters, the argument must be enclosed in double or single quotes, making it a “character string”: This is also necessary for a few words with syntactic meaning including if, for and function."

?`if`
?"if"       # same
help("if")  # same

There are also help pages for datasets, general topics and some packages.

?iris
?Syntax
?lubridate    

Use the example function to see examples of how to use it.

example(paste)
example(`for`)

The demo function gives longer demonstrations of how to use a function.

demo()                           # all demos in loaded pkgs
demo(package = .packages(all.available = TRUE)) # all demos
demo(plotmath)
demo(graphics)

Finding a function that you don't know the name of

Use ?? or, equivalently, help.search.

??regression
help.search("regression")

Again, non-standard names and phrases need to be quoted.

??"logistic regression"

apropos finds functions and variables in the current session-space (but not in installed but not-loaded packages) that match a regular expression.

apropos("z$") # all fns ending with "z"

rseek.org is an R search engine with a Firefox plugin.

RSiteSearch searches several sites directly from R.

findFn in sos wraps RSiteSearch returning the results as a HTML table.

RSiteSearch("logistic regression")

library(sos)
findFn("logistic regression")

Finding packages

available.packages tells you all the packages that are available in the repositories that you set via setRepositories. installed.packages tells you all the packages that you have installed in all the libraries specified in .libPaths. library (without any arguments) is similar, returning the names and tag-line of installed packages.

View(available.packages())
View(installed.packages())
library()
.libPaths()

Similarly, data with no arguments tells you which datasets are available on your machine.

data()

search tells you which packages have been loaded.

search()

packageDescription shows you the contents of a package's DESCRIPTION file. Likewise news read the NEWS file.

packageDescription("utils")    
news(package = "ggplot2")

Getting help on variables

ls lists the variables in an environment.

ls()                 # global environment
ls(all.names = TRUE) # including names beginning with '.'
ls("package:sp")     # everything for the sp package

Most variables can be inspected using str or summary.

str(sleep)
summary(sleep)

ls.str is like a combination of ls and str.

ls.str()
ls.str("package:grDevices")
lsf.str("package:grDevices")  # only functions    

For large variables (particularly data frames), the head function is useful for displaying the first few rows.

head(sleep)

args shows you the arguments for a function.

args(read.csv)

General learning about R

The Info page is a very comprehensive set of links to free R resources.

Many topics in R are documented via vignettes, listed with browseVignettes.

browseVignettes()
vignette("intro_sp", package = "sp")

By combining vignette with edit, you can get its code chunks in an editor.

edit(vignette("intro_sp",package="sp"))    
like image 111
Tomas Greif Avatar answered Nov 12 '22 11:11

Tomas Greif


This answer already gives you a very comprehensive list.

I would add that findFn("some search terms") in package sos is extremely helpful, if you only have an idea/keywords of what you are looking for and don't already have a package or function in mind.

And also the task views on CRAN: not really a search process but a great place to wander as you wonder.

like image 29
Bryan Hanson Avatar answered Nov 12 '22 09:11

Bryan Hanson


This thread contains many good suggestions. Let me add one more.

For finding which packages are loaded, plus extra goodies, ?sessionInfo is quite nice.

like image 2
gung - Reinstate Monica Avatar answered Nov 12 '22 09:11

gung - Reinstate Monica


help(package="<package-name>") where of course <package-name> is the name of the package you want help for.

like image 1
James Hirschorn Avatar answered Nov 12 '22 11:11

James Hirschorn