Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print R package function index to console

Tags:

r

I want to print the documentation for an R package to the console. Doing

utils:::.getHelpFile(help("print"))

works just fine but when I try

utils:::.getHelpFile(help(package="MASS"))

I get an error stating:

Error in dirname(file) : a character vector argument expected

So my question is: how can I print the documentation for an R package (i.e help(package="package_name")) to the console? Thanks in advance.

like image 640
petobens Avatar asked Aug 15 '15 19:08

petobens


People also ask

How do I print a function in R?

Print output using paste() function inside print() function R provides a method paste() to print output with string and variable together. This method defined inside the print() function. paste() converts its arguments to character strings. One can also use paste0() method.

How do I get help for a function in R?

Use the help() commandTo find information for a particular function, such as the function print, type help('print') on the R command line and press enter (I recommend using quotes whenever you use this command, but there are some special cases when they are unnecessary).

Which one of the following function is can be used to list the installed packages in R?

You can use lsf.


2 Answers

help(package = "MASS") takes you to the INDEX file for the MASS package, opened in a browser window (depending on your settings). To read that file into the console, we can use system.file() to get the file path, then readLines() to read it as a character vector.

## get the complete file path for the index file of the MASS package
f <- system.file("INDEX", package = "MASS")
## read it
readLines(f)
# [1] "Functions:"                                  
# [2] "========="
# [3] ""
# [4] "Null                    Null Spaces of Matrices"
# [5] "addterm                 Try All One-Term Additions to a Model"
# [6] "anova.negbin            Likelihood Ratio Tests for Negative Binomial GLMs"
# ...
# ...

Or we can wrap it in cat() to get a cleaner version

cat(readLines(f), sep = "\n")
# Functions:
# =========
#
# Null                    Null Spaces of Matrices
# addterm                 Try All One-Term Additions to a Model
# anova.negbin            Likelihood Ratio Tests for Negative Binomial GLMs
# ...
# ...

Alternatively, you could get the same result with

readLines(file.path(find.package("MASS"), "INDEX"))

Finally, if you're wondering about the links to the package description and news that appear at the top of the html browser, those can be obtained with

packageDescription("MASS")
news(package = "MASS")
like image 63
Rich Scriven Avatar answered Sep 21 '22 18:09

Rich Scriven


utils:::.getHelpFile(help(package="MASS")) doesn't work because help(pacakge="MASS") returns a packageInfo class object, not a help_files_with_topic class object (which is a full file path with some other attributes).

Here's the simplest thing I can come up with:

cat(paste(format(help(package="MASS", help_type="text")), collapse="\n"),"\n")

Basically, you format the output from help(package="MASS"). Then paste that result into a single character string, collapsed together by newlines. Then call cat on that result.

like image 29
Joshua Ulrich Avatar answered Sep 20 '22 18:09

Joshua Ulrich