Search code examples
rlatexnoamtools

R function help in `.tex` or `.Rnw` format


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

Solution

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