将 R 帮助文件小插图作为输出打印到 R HTML 笔记本中
Print an R help file vignette as output into an R HTML notebook
我正在尝试在 R 笔记本块中打印 R 帮助文件小插图,以输出到 HTML 文件中。我希望整个小插图在 HTML 笔记本预览中显示为输出,因为它可以作为快速回归示例的良好数据字典。使用 library(mlbench)
,我试过:
print(?BostonHousing2)
我试着打电话给
?BostonHousing2
在代码块中,它们都没有输出到 HTML 文件,它们只是填充在 RStudio 的“帮助”选项卡中。
有人有什么想法吗?
这是一个方法(如果我正确理解你想要的)。但也许不是最好的。
---
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")
```
基于Noam Ross's solution,并修改了Stéphane Laurent's answer中的部分代码,以下函数可用于returnHTML版本的帮助文件
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
#
# print-an-r-help-file-vignette-as-output-into-an-r-html-notebook
# and Michael Sumner (mdsumner)
#
# 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)
}
然后可以在 RMarkdown 文档中使用,准备编织:
```{r, echo = FALSE, results = "asis"}
help_console("pretty", "base", format = "html")
```
和往常一样,R chunk需要设置为results = "asis"
才能输出到HTML。
此解决方案不需要 gbRd
包。
出于某些奇怪的原因,我可以从 RStudio 的 Knit 下拉菜单中 Knit to html_vignette
,但是 不能 成功 Build Document
来自 RStudio 菜单,启用 Project Options - Build Tools - Roxygen Configure - Vignettes
,错误
Error: Sections \title, and \name must exist and be unique in Rd files
尝试使用 help_console
函数时抛出。
但是,我能够成功地创建 HTML 小插图并合并到带有 的包中,或者构建源代码或二进制包(构建 source/binary 包可以从 RStudio 的 "build" 菜单完成。
我正在尝试在 R 笔记本块中打印 R 帮助文件小插图,以输出到 HTML 文件中。我希望整个小插图在 HTML 笔记本预览中显示为输出,因为它可以作为快速回归示例的良好数据字典。使用 library(mlbench)
,我试过:
print(?BostonHousing2)
我试着打电话给
?BostonHousing2
在代码块中,它们都没有输出到 HTML 文件,它们只是填充在 RStudio 的“帮助”选项卡中。
有人有什么想法吗?
这是一个方法(如果我正确理解你想要的)。但也许不是最好的。
---
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")
```
基于Noam Ross's solution,并修改了Stéphane Laurent's answer中的部分代码,以下函数可用于returnHTML版本的帮助文件
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
#
# print-an-r-help-file-vignette-as-output-into-an-r-html-notebook
# and Michael Sumner (mdsumner)
#
# 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)
}
然后可以在 RMarkdown 文档中使用,准备编织:
```{r, echo = FALSE, results = "asis"}
help_console("pretty", "base", format = "html")
```
和往常一样,R chunk需要设置为results = "asis"
才能输出到HTML。
此解决方案不需要 gbRd
包。
出于某些奇怪的原因,我可以从 RStudio 的 Knit 下拉菜单中 Knit to html_vignette
,但是 不能 成功 Build Document
来自 RStudio 菜单,启用 Project Options - Build Tools - Roxygen Configure - Vignettes
,错误
Error: Sections \title, and \name must exist and be unique in Rd files
尝试使用 help_console
函数时抛出。
但是,我能够成功地创建 HTML 小插图并合并到带有