4

I can get help on any R function in html format using ? or help(). I wonder if I can get help on R functions in .tex or .Rnw format to use in .tex dociument. Thanks in advance for your help.

?lm
MYaseen208
  • 22,666
  • 37
  • 165
  • 309

2 Answers2

6

I found a blog post by Noam Ross that references this problem here: http://www.r-bloggers.com/printing-r-help-files-in-the-console-or-in-knitr-documents/

The function is available in Noam's package noamtools available via github

 library(devtools)
 install_github("noamtools", "noamross")
 library(noamtools)
 help_console(lm, format = "latex")

For sake of posterity the function they create is

help_console <- function(topic, format=c("text", "html", "latex", "Rd"),
                         lines=NULL, before=NULL, after=NULL) {  
  format=match.arg(format)
  if (!is.character(topic)) topic <- deparse(substitute(topic))
  helpfile = utils:::.getHelpFile(help(topic))

  hs <- capture.output(switch(format, 
                              text=tools:::Rd2txt(helpfile),
                              html=tools:::Rd2HTML(helpfile),
                              latex=tools:::Rd2latex(helpfile),
                              Rd=tools:::prepare_Rd(helpfile)
                              )
                      )
  if(!is.null(lines)) hs <- hs[lines]
  hs <- c(before, hs, after)
  cat(hs, sep="\n")
  invisible(hs)
}

Use it like this to get latex output

help_console(lm, format = "latex")
Dason
  • 60,663
  • 9
  • 131
  • 148
  • (+1): This is a nice solution. Could shed light how to save the text rather than showing in R console. Thanks for your help. – MYaseen208 Jan 07 '14 at 13:23
  • You can store the results into a variable as is `results <- help_console(lm, format = "latex")` the way the function is coded it will still display on the screen (if you don't want that then comment out the 'cat' line). If you want to save to a file you could then cat the results out to a file `cat(results, file = your_file)` – Dason Jan 07 '14 at 14:01
  • Using `cat(results, file = your_file)` changes the type of text and not working properly. – MYaseen208 Jan 08 '14 at 15:57
  • What do you mean? Works fine for me - the latex code is displayed on the console. I mean you have to make sure you change your_file to a valid file name though... – Dason Jan 09 '14 at 05:49
  • Thanks @Dason for your help, Can you help me to get help in `tex` format on `corClasses` function from `nlme` package? I tried the code `help_console(topic=corClasses, format = "latex")` but it seems it is not working. See [here](http://stackoverflow.com/q/21046970/707145). – MYaseen208 Jan 19 '14 at 11:21
  • 1
    It's always safer with this function to wrap the argument in quotes: `help_console(topic="corClasses", format = "latex")`. Especially in a case like this where corClasses isn't actually a function. – Dason Jan 19 '14 at 17:04
  • Thanks @Dason for your help. Much appreciated. Please have a look on this [question](http://stackoverflow.com/q/21046970/707145) and guide me on author's email problem if you can. Thanks – MYaseen208 Jan 19 '14 at 17:12
0

From ?help ,

The following types of help are available:

Plain text help

HTML help pages with hyperlinks to other topics, shown in a browser by browseURL. If for some reason HTML help is unavailable (see startDynamicHelp), plain text help will be used instead.

For help only, typeset as PDF – see the section on ‘Offline help’.

So you'd have to work from one of those formats.

Community
  • 1
  • 1
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73