Search code examples
rsweave

How to include output of help() in sweave pdf


I would like to include function documentation from the help file in a sweave document. I tried the following sweave block

<<>>=
?lm
@

but I get error messages when calling Sweave on the Rnw file. How can I include the entire help message in the document?


Solution

  • The key to this is really figuring out how to get the information you desire as a character string.

    • help("lm") opens up the help file for the relevant function, but not in the console.
    • utils:::.getHelpFile gives you the Rd version of that file.
    • From there, you can use tools:::Rd2txt to convert it to text...
    • Which can be "captured" using capture.output.

    Those are essentially the steps contained in the first few lines of helpExtract from my "SOfun" package. That function, however, captures just the requested section.

    Instead, if you can settle for just the text, you can do something along the lines of:

    gsub("_\b", "", 
        capture.output(tools:::Rd2txt(
            utils:::.getHelpFile(utils::help("lm")))))