Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I extract text from R's help command? [duplicate]

Tags:

r

Possible Duplicate:
R help page as object

I can do

temp <- help('ls')

But I can't get a handle on this object. I don't think there's much meat in it except a call is there? unclass, str, attributes don't seem to lead anywhere.

I would like to, for example,

(1) Extract the text of the Details section of the help for ls; and

(2) Extract all the text into one big string.

Any ideas? Thanks

like image 308
Xu Wang Avatar asked Feb 08 '12 11:02

Xu Wang


1 Answers

help itself doesn't return anything useful. To get the help text, you can read the contents of the help database for a package, and parse that.

extract_help <- function(pkg, fn = NULL, to = c("txt", "html", "latex", "ex"))
{
  to <- match.arg(to)
  rdbfile <- file.path(find.package(pkg), "help", pkg)
  rdb <- tools:::fetchRdDB(rdbfile, key = fn)
  convertor <- switch(to, 
      txt   = tools::Rd2txt, 
      html  = tools::Rd2HTML, 
      latex = tools::Rd2latex, 
      ex    = tools::Rd2ex
  )
  f <- function(x) capture.output(convertor(x))
  if(is.null(fn)) lapply(rdb, f) else f(rdb)
}

pkg is a character string giving the name of a package
fn is a character string giving the name of a function within that package. If it is left as NULL, then the help for all the functions in that package gets returned.
to converts the help file to txt, tml or whatever.

Example usage:

#Everything in utils
extract_help("utils")

#just one function
extract_help("utils", "browseURL")

#convert to html instead
extract_help("utils", "browseURL", "html")

#a non-base package 
extract_help("plyr")
like image 139
Richie Cotton Avatar answered Oct 19 '22 22:10

Richie Cotton