Search code examples
rr-markdownrnotebook

Print an R help file vignette as output into an R HTML notebook


I'm trying to print an R help file vignette in an R notebook chunk for output into an HTML file. I want the entire vignette to show up as output in the HTML notebook preview because it serves as a nice data dictionary for a quick regression example. Using library(mlbench), I tried:

print(?BostonHousing2)

and I tried just calling

?BostonHousing2

in the code chunks, and neither of them output to the HTML file, they just populate in the Help tab inside of RStudio.

Anyone have any ideas?


Solution

  • Here is a way (if I correctly understand what you want). But maybe not the best one.

    ---
    title: "Untitled"
    author: "Stéphane Laurent"
    date: "29 février 2020"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    library(gbRd) # for Rd_fun
    ```
    
    ```{r, results='asis'}
    Rd <- Rd_fun(help("pretty")) 
    htmlfile <- tempfile(fileext = ".html")
    tools::Rd2HTML(Rd, htmlfile, package = "",
                   stages = c("install", "render"))
    htmllines <- readLines(htmlfile)
    i <- grep("<body>", htmllines)
    j <- grep("</body>", htmllines)
    cat(htmllines[(i+1):(j-1)], sep = "\n")
    ```