How to get help in R? - r

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?

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"))

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.

This thread contains many good suggestions. Let me add one more.
For finding which packages are loaded, plus extra goodies, ?sessionInfo is quite nice.

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

Often the same function name is used by several packages. To get help on a function from a specific package, use:
help(aggregate, package="stats")
help(aggregate, package="sp")

In the RStudio IDE you can click on any function name and press F1, which will directly open the associated function help text in its pane. Like you would have called help() or ?fun().

Related

Julia's equivalent to R's ?? (double question-mark help.search across all packages)

In R you can search the documentation by typing a question mark ? or a double question mark ??. How do you search the manual for strings in the Julia REPL?
>?first
No documentation for ‘first’ in specified packages and libraries:
you could try ‘??first’
>??first
In the R console a browser window opens up:
In RStudio the page is opened within the IDE.
The help() function and ? help operator in R provide access to the
documentation pages for R functions, data sets, and other objects,
both for packages in the standard R distribution and for contributed
packages.
The help() function and ? operator are useful only if you already know the name of the function that you wish to use. Other ways to search include apropos and ??
The apropos() function searches for objects, including functions,
directly accessible in the current R session that have names that
include a specified character string.
The ?? operator is a synonym for help.search(). The help.search()
function scans the documentation for packages installed in your
library. The argument to help.search() is a character string or regular expression.
P.S. I intend to answer my own question.
Julia has similar interactive utilties. Julia's main search utility for docstrings is named apropos.
To search the documentation for information about "first" in Julia, you have apropos("first") or equivalently ?"first". Thus ?"first" is roughly equivalent to R's ??.
To search for functions and methods, you can type a singe question mark ?, just as with R. In the Julia REPL, as you type the question mark, the prompt changes to a question mark. If you type "first" it searches through strings, while if you type first without quote-marks, you get a search over variables exported by the modules currently loaded.
Illustration:
help?>"first"
# or equivalently:
julia>apropos("first")
help?>first
If you search for a string, case is ignored. If you want to search within the DataFrames module, type using DataFrames before searching.
This also works in Visual Studio Code and Atom:

chapters in R reference manual and documentation using roxygen2

Once I've seen a package supporting R program in which his documentation and reference manual was divided into chapters ( they were called by the consecutive letters of the alphabet ) so that an user could see that functions presented by that package are divided into groups.
Now I'm trying to do the same. I'm building package using roxygen2 package but can not deal with that problem.
Can any1 has any solution ? So in that case my documentation will be split to a file like this:
Group1:
funAX
funBX
funCX
Group2:
funAY
funBY
funCY
instead of
funAX
funAY
funBX
funBY
funCX
funCY
A name of package that provides that solution will also be helpful.
In the lattice package developers invented a way to order a documentation. They simply add letters in an alfabetic order to the beginning of a .Rd file. This order might suggest the way of passing thru the manual to optimize understanding. Manual is here.

plot.predict() or predict.plot() functions cited in literature, but no longer exists

I am trying to use predict.plot or plot.predict (I've seen both of them referenced on various websites and I don't know which one is right).
However, in R, neither of these are valid functions. I don't know if I'm missing a package or if the sources I'm using are outdated in terms of the functions being referenced.
This site is using the function
It's very old. Can someone familiar with stats help me figure out how to do this with the latest version of R, or help me figure out how to get predict.plot/plot.predict to work?
This is a user defined function. The author of the course you linked to provides it online:
Link to R script
The S3 dispatch system for functions in R examines the class of the first argument and then calls a function with the name func.class. In this case the author has defined several plot.predict functions: predict.plot.data.frame, predict.plot.lm, and predict.plot.formula which are then given the arguments on hte basis of the class of what is (first) in the argument list. The plot.predict function is just this:
predict.plot <- function(object, ...) UseMethod("predict.plot")
The "good stuff" is in the other three functions at the link Roland provided. I do think the author's use of a dot in the name for the generic function is a bit confusing. One might have expected there to be a class "plot" for which there was a generic function predict, but that is not really the case here, although you might find it interesting to just type: methods(predict) .After you had loaded the R script on that website you could find those various functions using:
methods(predict.plot)
#[1] predict.plot.data.frame predict.plot.formula predict.plot.lm

What is read.data in R? Why can't I find it in any docs?

I'm trying to debug my first R script and I came across this line:
data <- read.data(dir, indiv, label)
I've been googling "R read.data" for the past 30 minutes and absolutely nothing is coming up. Am I doing something wrong? Is there a good way to look up things I see in R scripts that I don't know what they are?
And what is this particular line doing anyway?
It's probably a function defined by the author of the script. Search for it in the code you have.
A couple of things to check:
Does your script define the function 'read.data' somewhere? read.data <- function(...
Does your script use library() or require() to load another package? In that case, the read.data function can be defined in that package.
Does your script use source to read another script? Check that script then...
package sos to the rescue:
read.data is a deprecated function in the rjags package
> library(sos)
> findFn("read.data")
Finds this result:
http://finzi.psych.upenn.edu/R/library/rjags/html/read.data.html
From this page:
Read data for a JAGS model from a file.
Usage
read.jagsdata(file)
read.bugsdata(file)
Note
Earlier versions of the rjags package had a read.data function which read data
in either format, but the function name was ambiguous (There are many data file
format in R) so this is now deprecated.
There isn't a base function named read.data. If you want to find help for an R function (for example read.table), simply type ?read.table at the interactive prompt.
This line calls a read.data function which is either defined in that script or in something else it loads (such as libraries with the library() or require(), other scripts with source()). You'll need to search those sources to find this function.

How to access the help/documentation .rd source files in R?

In R, one very neat feature is that the source code of functions is accessible as objects in the workspace.
Thus, if I wanted to know the source code of, for example, grep() I can simply type grep into the console and read the code.
Similarly, I can read the documentation for grep by typing ?grep into the console.
Question: How can I get the source code for the documentation of a function? In other words, where do I find the .rd files?
I find studying the source of well-written code an excellent way of learning the idioms. Now I want to study how to write documentation for some very specific cases. I have not been able to find the documentation files for any of the base R functions in my R installation. Perhaps I have been looking in the wrong place.
It seems you can extract the Rd sources from an installed R. I'm using R-devel (2011-09-05 r56942).
Get the database of Rd for the base package.
library(tools)
db <- Rd_db("base")
Search for "grep.Rd" in the names of the Rd DB, for example:
grep("grep.Rd", names(db), value = TRUE)
[1] "d:/murdoch/recent/R64/src/library/base/man/agrep.Rd"
[2] "d:/murdoch/recent/R64/src/library/base/man/grep.Rd"
Get just the Rd object for grep.
db[grep("/grep.Rd", names(db))]
$`d:/murdoch/recent/R64/src/library/base/man/grep.Rd`
\title{Pattern Matching and Replacement}
\name{grep}
\alias{grep}
\alias{grepl}
\alias{sub}
\alias{gsub}
\alias{regexpr}
\alias{gregexpr}
\alias{regexec}
\keyword{character}
\keyword{utilities}
\description{
\code{grep}, \code{grepl}, \code{regexpr} and \code{gregexpr} search
for matches to argument \code{pattern} within each element of a
character vector: they differ in the format of and amount of detail in
the results.
\code{sub} and \code{gsub} perform replacement of the first and all
matches respectively.
}\usage{
...
...
There are tools for getting the components from the Rd objects, so you can refine searching to keywords or name, see examples in ?Rd_db and try this.
lapply(db, tools:::.Rd_get_metadata, "name")

Resources