【问题标题】:Rmarkdown in loop, "knit html" button different output than rmarkdown::renderRmarkdown 在循环中,“knit html”按钮的输出与 rmarkdown::render 不同
【发布时间】:2018-07-18 11:10:05
【问题描述】:

编辑

此问题并非(必然)仅与 {htmlTable} 相关。渲染一个使用 {kableExtra} 包 (see also this thread) 设置样式的 html 表格时也会发生同样的情况。

原始问题

我在 RMarkdown 中工作,尝试在循环中使用 htmlTable 包中的 htmltables 呈现文档。当我在 RStudio 中按下“Knit”按钮时,一切正常。但是当我使用render() 函数时,循环中的htmltables 不会被打印,但循环之前的表格会打印。 我看了那个“knit”按钮,渲染功能也是这样,所以我不明白怎么了?

这是带有hmtlTables 的.Rmd 文件的工作示例:

---
output: html_document
---

## htmlTables


```{r, results='asis'}
library(data.table)
library(htmlTable)
library(ggplot2)
a <- data.table(matrix(seq(1, 100, 5), ncol = 2))
htmlTable(a)
for (i in 10:11) {
    cat( paste('## title no.', i, '\n' ) ) 
    print(ggplot(a*i, aes(x = V1, y = V2)) + geom_point())
    print(htmlTable(a*i))
    cat('\n')
}
```

我需要使用不同的参数自动生成许多报告,因此我需要使用 render() 来实现这一点。

【问题讨论】:

标签: html r r-markdown rstudio knitr


【解决方案1】:

这取决于查看器的检测方式。
要检查这个:

---
title: "Check viewer"
output: html_document
---

```{r viewer, echo=FALSE}
getOption("viewer")
``` 

带有knit按钮:

Check viewer
## NULL

与rmarkdown::render:


Check viewer

## function (url, height = NULL) 
## {
##     if (!is.character(url) || (length(url) != 1)) 
##         stop("url must be a single element character vector.", 
##             call. = FALSE)
##     if (identical(height, "maximize")) 
##         height <- -1
##     if (!is.null(height) && (!is.numeric(height) || (length(height) != 
##         1))) 
##         stop("height must be a single element numeric vector or 'maximize'.", 
##             call. = FALSE)
##     invisible(.Call("rs_viewer", url, height, PACKAGE = "(embedding)"))
## }
## <environment: 0x0000020ccb7f26d8>

knit 按钮在没有定义查看器的环境中执行渲染,而 RStudio 控制台中的rmardown::render 使用交互式查看器。

htmlTable 允许设置options(htmlTable.cat = TRUE),以便print 工作,即不使用交互式查看器,请参阅?htmlTable::htmlTable。

打印函数依赖于 [base::interactive()] 函数 用于确定是否应将输出发送到浏览器或终端。 在小插图和其他直接编织的文档中,您可能需要设置 useViewer = FALSE 或者设置options(htmlTable.cat = TRUE)。

---
output: html_document
---

## htmlTables


```{r, results='asis'}
options(htmlTable.cat = TRUE)
library(data.table)
library(htmlTable)
library(ggplot2)
a <- data.table(matrix(seq(1, 100, 5), ncol = 2))
htmlTable(a)
for (i in 10:11) {
    cat( paste('## title no.', i, '\n' ) ) 
    print(ggplot(a*i, aes(x = V1, y = V2)) + geom_point())
    print(htmlTable(a*i))
    cat('\n')
}
```

【讨论】:

  • @tjebo,print的解决方案
  • 感谢瓦尔迪。我会检查这个。但是,据我所知,例如 kableExtra 没有使用 htmlTable ,这里的问题也是一样的。并且更改选项仍然不能真正解释为什么 knit 按钮和直接函数调用会导致不同的输出......
  • @tjebo,相关帖子:stackoverflow.com/a/22871109/13513328 - 上述选项打开cat,而不是触发查看器。
  • @tjebo,查看更新原因?
【解决方案2】:

htmlTable 定义了一个print 方法,它似乎在新窗口中打印表格,将print() 更改为writeLines() 即可:

---
output: html_document
---

## htmlTables


```{r, results='asis'}
library(data.table)
library(htmlTable)
library(ggplot2)
a <- data.table(matrix(seq(1, 100, 5), ncol = 2))
htmlTable(a)
for (i in 10:11) {
    cat( paste('## title no.', i, '\n' ) ) 
    print(ggplot(a*i, aes(x = V1, y = V2)) + geom_point())
    writeLines(htmlTable(a*i))
    cat('\n')
}
```

但是对我来说仍然有点奇怪,因为我相信 knit 按钮在后台使用 rmarkdown::render() 但在这种情况下它们具有不同的行为。

【讨论】:

  • 你用它拯救了我的一天。我对这种行为感到非常惊讶,也许聪明的人知道原因并会获得赏金:)
猜你喜欢
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 2022-08-06
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多