我希望在Shiny应用程序的PDF报告制作中使用kableExtra
软件包功能(如此处所述).不幸的是,虽然我能够复制上面在Shiny环境之外链接的文档中的示例,但是当我在Shiny中尝试它时会出现以下错误:
"C:/ Program Files/RStudio/bin/pandoc/pandoc"+ RTS -K512m -RTS report.utf8.md --to latex --from markdown + autolink_bare_uris + ascii_identifiers + tex_math_single_backslash --output pandoc2f0831607c9a.pdf --template" C:\ Users\PStraforelli\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex"--highlight-style tango --latex-engine pdflatex --variable graphics = yes - 变量"几何:margin = 1in"!未定义的控制序列.l.160\toprule
pandoc.exe:生成PDF时出错警告:运行命令'"C:/ Program Files/RStudio/bin/pandoc/pandoc"+ RTS -K512m -RTS report.utf8.md --to latex --from markdown + autolink_bare_uris + ascii_identifiers + tex_math_single_backslash --output pandoc2f0831607c9a.pdf --template"C:\ Users\phil\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex"--highlight-style tango - -latex-engine pdflatex --variable graphics = yes --variable"geometry:margin = 1in"'状态为43警告:错误:pandoc文档转换失败,错误43堆栈跟踪(最里面的第一个):53:pandoc_convert 52:转换51:rmarkdown :: render 50:下载$ func [C:\ Users\phil\Desktop\app/app.R#16] 1:shiny :: runApp错误:pandoc文档转换失败,错误43
这是一个可重复的例子:
app.R文件:
#From here: http://shiny.rstudio-staging.com/articles/generating-reports.html
shinyApp(
ui = fluidPage(
sliderInput("slider", "Slider", 1, 32, 10),
downloadButton("report", "Generate report")
),
server = function(input, output) {
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(n = input$slider)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)
Run Code Online (Sandbox Code Playgroud)
report.Rmd文件:
---
title: "Dynamic report"
output: pdf_document
params:
n: NA
---
```{r}
#From here: http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
library(magrittr)
library(knitr)
library(kableExtra)
dt <- mtcars[1:params$n, 1:6]
kable(dt, format = "latex", booktabs = T) %>%
kable_styling(latex_options = "striped")
```
Run Code Online (Sandbox Code Playgroud)
当我将app.R文件启动到Internet浏览器上,然后单击"生成报告"时,我收到上面引用的错误.如果我删除文件(和函数)中的format = "latex"
参数knitr::kable()
,那么一切正常.report.Rmd
kable_styling()
你需要放入library(kableExtra)
你的rmarkdown.它会自动为您加载必要的LaTeX软件包.
更新:rmarkdown render在清理元数据方面存在一些问题.要确保加载这些包,您可以按照晕影中"此包中使用的LaTeX包"部分中的指示并将其放在header-includes
yaml标题中.
请参阅为什么从rmarkdown渲染pdf需要在渲染之间关闭rstudio?详情.