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.
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.
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).
You can use lsf.
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With