渲染为 PDF 时 Markdown 中出现 kableExtra 错误?

rje*_*jen 2 pdf r r-markdown kableextra

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

R降价编译错误: 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)
Run Code Online (Sandbox Code Playgroud)

制作 Rmd 文件:

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

tibble(x = 1, y = 2, z = 3) %>%
    kable()
Run Code Online (Sandbox Code Playgroud)

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

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

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

x
y
z
1
2
3
Run Code Online (Sandbox Code Playgroud)

运行后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')
Run Code Online (Sandbox Code Playgroud)

这将返回:

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.
Run Code Online (Sandbox Code Playgroud)

是什么阻止我使用kableExtra

eip*_*i10 7

将kableExtra 与 kable 用于 PDF 输出时format="latex",您需要在 kable 函数中显式添加(例如,kable(format="latex"))。见kableExtra引进更多的信息和示例。

kableExtraPDF输出暗角表明您不需要设置format="latex"为0.9.0或更高版本,但我发现(使用1.1.0版本目前的),我还需要设置format="latex"或我得到的html默认输出。

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