【问题标题】:kableExtra error in Markdown when rendering to PDF?渲染为 PDF 时 Markdown 出现 kableExtra 错误?
【发布时间】:2020-01-31 21:25:11
【问题描述】:

在尝试通过从 R 脚本渲染 Rmd 文件来创建 PDF 时,我似乎在使用 kableExtra 时遇到了问题。我尝试按照以下说明操作没有成功:

R markdown compile error: https://github.com/rstudio/bookdown/issues/440 https://community.rstudio.com/t/rendering-both-pdf-and-html-is-only-possible-interactively-not-via-script/19520/3 https://github.com/haozhu233/kableExtra/issues/301

我将通过脚本访问包:

library(tidyverse)
library(knitr)
library(rmarkdown)
library(tinytex)

制作 Rmd 文件:

---
output: pdf_document 
---
{r, comment = NA, echo = FALSE, message = FALSE, warning = FALSE}

tibble(x = 1, y = 2, z = 3) %>%
    kable()

使用以下脚本渲染 Rmd 文件:

render('C:/Users/Rasmus/SO/Test.Rmd',
    output_file = "Test.pdf",
    output_format = 'pdf_document',
    output_dir = 'C:/Users/Rasmus/SO')

这给了我一个带有表格的 PDF。但是,如果我开始与其他人一起运行library(kableExtra),然后应用渲染过程,我会得到一个 PDF:

x
y
z
1
2
3

运行library(kableExtra)后,我尝试了以下Rmd文件的渲染过程:

---
output: pdf_document 
---
{r, comment = NA, echo = FALSE, message = FALSE, warning = FALSE}

tibble(x = 1, y = 2, z = 3) %>%
    kable() %>%
    kable_styling(latex_options = 'scale_down')

这会返回:

output file: Test.knit.md

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

是什么阻止我使用kableExtra

【问题讨论】:

标签: r pdf r-markdown kableextra


【解决方案1】:

在将 kableExtra 与 kable 一起用于 PDF 输出时,您需要在 kable 函数中显式添加 format="latex"(例如,kable(format="latex"))。有关更多信息和示例,请参阅 the kableExtra introduction

kableExtra PDF output Vignette 表示 0.9.0 或更高版本不需要设置 format="latex",但我发现(目前使用 1.1.0 版本)我仍然需要设置 format="latex" 或我默认得到 html 输出。

您可以在任何脚本或 Rmarkdown 文档的开头运行 options(knitr.table.format = "latex") 以使 Latex 成为该 R 会话的默认输出,并避免将 format="latex" 添加到每个单独的 kable 表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    相关资源
    最近更新 更多