Reputation: 125
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?
Upvotes: 1
Views: 1000
Reputation: 546
Based on Noam Ross's solution, and modified with some of the code in Stéphane Laurent's answer, the following function can be used to return an HTML version of the help file
help_console <- function(topic, package,
format=c("text", "html", "latex", "Rd"),
before=NULL, after=NULL) {
# topic - the command for which help is required
# package - the package name with the required topic
# format - output format
# before - place code before the output e.g. "<blockquote>"
# after - place code after the output e.g. "</blockquote>"
# based on code by Noam Ross
# http://www.noamross.net/archives/2013-06-18-helpconsoleexample/
# Stéphane Laurent
# https://stackoverflow.com/questions/60468080/
# print-an-r-help-file-vignette-as-output-into-an-r-html-notebook
# and Michael Sumner (mdsumner)
# https://stackoverflow.com/questions/7495685/
# how-to-access-the-help-documentation-rd-source-files-in-r
format <- match.arg(format)
if (!is.character(topic)) topic <- deparse(substitute(topic))
db <- tools::Rd_db(package)
helpfile <- db[paste0(topic, ".Rd")][[1]]
hs <- capture.output(
switch(
format,
text = tools::Rd2txt(helpfile),
html = tools::Rd2HTML(
helpfile,
package = "",
stages = c("install", "render")
),
latex = tools::Rd2latex(helpfile),
Rd = tools:::prepare_Rd(helpfile)
)
)
if (format == "html") {
i <- grep("<body>", hs)
j <- grep("</body>", hs)
hs <- hs[(i+1):(j-1)]
}
hs <- c(before, hs, after)
hs <- cat(hs, sep = "\n")
invisible(hs)
}
Which then can be used as follow in a RMarkdown document, ready for knitting:
```{r, echo = FALSE, results = "asis"}
help_console("pretty", "base", format = "html")
```
As usual, the R chunk needs to be set to results = "asis"
to output to HTML.
This solution does not require the gbRd
package.
For some strange reason, I can Knit to html_vignette
from RStudio's Knit dropdown, but cannot successfully Build Document
from the RStudio menu with Project Options - Build Tools - Roxygen Configure - Vignettes
enabled, with the error
Error: Sections \title, and \name must exist and be unique in Rd files
thrown when trying to use the help_console
function.
However, I was able to successfully able to create the HTML vignette and incorporate into a package with A-breeze's solution, or building a source or binary package (building source/binary packages can be done from RStudio's "build" menu).
Upvotes: 1
Reputation: 84529
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")
```
Upvotes: 2