为什么从 rmarkdown 渲染 pdf 需要在渲染之间关闭 rstudio? [英] Why does rendering a pdf from rmarkdown require closing rstudio between renders?

查看:66
本文介绍了为什么从 rmarkdown 渲染 pdf 需要在渲染之间关闭 rstudio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个使用 render() 呈现的 rmarkdown 文档.render 调用有两个参数化的元素:

I am trying to make a rmarkdown document that is rendered using render(). The render call has two elements that are parameterized:

  • 我希望用户能够指定 pdf 或 html.直接使用 output_format().
  • 我还想向文档传递一个参数,以指定表(使用 kableExtra 包)是 Latex 还是 html.
  • I would like the user to be able to specify pdf or html. Straightforward using output_format().
  • I also would like to pass a parameter to the document to specify if the tables (using the kableExtra package) are latex or html.

这是名为 test.Rmd 的 rmarkdown 文件

This is the rmarkdown file called test.Rmd

---
title: "Title"
author: "Zzz Zzzzzz"
params:
  table_format:
    value
---

```{r setup}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)

options(knitr.table.format = params$table_format) 
```

## Test table

```{r cars}
if (params$table_format == "latex"){
kable(iris[1:100,], booktabs = T) %>%
  kable_styling(latex_options = c("scale_down")) 
}

if (params$table_format == "html"){
kable(iris[1:100,]) %>%
  kable_styling(bootstrap_options = c("striped", "hover"))  %>%
  scroll_box(width = "500px", height = "600px")
}


params$table_format
```

现在是渲染文件的两个调用:

Now here are the two calls to render the file:

rmarkdown::render("test.Rmd", output_format = "pdf_document", params = list(
  table_format = "latex"
))


rmarkdown::render("test.Rmd", output_format = "html_document", params = list(
  table_format = "html"
))

问题

现在,如果我打开一个新的 rstudio 会话,我可以运行两个 render 调用没问题.创建 .pdf 或 .html 文件.但是,如果我然后尝试运行 .pdf 渲染再次,我会收到以下错误:

Problem

Now if I open up a fresh rstudio session I can run both render calls no problem. Either .pdf or an .html file is created. However if I then try to run the .pdf render again i get the following error:

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTStest.utf8.md --to 乳胶 --frommarkdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash--output test.pdf --template "C:\Users\salbers\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" !未定义的控件顺序.\begin {tabular}{rrrrl} \toprule萼片长度萼片宽度&宠物... l.267 \end{tabular}}

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\salbers\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" ! Undefined control sequence. \begin {tabular}{rrrrl} \toprule Sepal.Length & Sepal.Width & Pet... l.267 \end{tabular}}

pandoc.exe:生成 PDF 错误错误:pandoc 文档转换失败,错误 43 此外:警告消息:运行命令'"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTStest.utf8.md --to 乳胶 --frommarkdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash--output test.pdf --template "C:\Users\salbers\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.exe: Error producing PDF Error: pandoc document conversion failed with error 43 In addition: Warning message: running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\salbers\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"' had status 43

html 渲染一遍又一遍地工作正常.如果我关闭 rstudio,然后重新启动项目,pdf 的渲染也可以正常工作.

The html render works fine over and over again. If I close rstudio, then ropen the project, the render for pdf work fine as well.

  • 谁能告诉我为什么我的 pdf 呈现的 rmarkdown 文档不能在 rstudio 的单个会话中呈现两次?
  • 同样,为什么 rstudio 必须在渲染之间关闭?

推荐答案

2019-01-21 更新:

2019-01-21 UPDATE:

knit 按钮和 render 函数之间的一大区别在于,knit 按钮总是从一个新环境"开始(正如我们所有人都能感觉到的),而 render 函数默认在 parent.env() 中启动.

One of the big difference between the knit button and the render function is that, the knit button always starts with a "new environment" (as we all can feel) while the render function by default starts in the parent.env().

render(input, ..., envir = parent.frame(), ...)

在函数文档中,我们看到

In the function documentation, we see

envir   
The environment in which the code chunks are to be evaluated 
during knitting (can use new.env() to guarantee an empty new 
environment).

因此,我们可以通过在渲染调用中放置 envir = new.nev() 来模拟点击 knit 按钮的行为.

Therefore, we can simulate the behavior of clicking the knit button by putting envir = new.nev() in the render call.

原答案:

嗯,让我先发布解决方案.要解决此行为,您需要将以下内容放入 yaml 部分.我还在本周早些时候的开发版本中添加了一个函数 kableExtra_latex_packages() 以显示以下文本.

Hmm, let me post the solution first. To solve this behavior, you need to put the following stuff in your yaml section. I also added a function kableExtra_latex_packages() in the dev version earlier this week to bring up the following texts.

header-includes:
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{array}
  - \usepackage{multirow}
  - \usepackage[table]{xcolor}
  - \usepackage{wrapfig}
  - \usepackage{float}
  - \usepackage{colortbl}
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable}
  - \usepackage[normalem]{ulem}

如果您想知道为什么会有如此奇怪的行为,这里有一个简短的解释.当您第一次在 rmarkdown 环境中加载 kableExtra 时,它会 尝试使用 usepackage_latex() 函数 随此包提供.如果你只是点击编织按钮,它工作正常,因为每个编织+渲染"过程都应该是孤立的.然而,令人惊讶的是(顺便说一句,感谢您提出它),正如我们在这里看到的,如果您尝试从控制台渲染,因为(我的假设)knitr 或 rmarkdown 试图重用一些缓存的结果,所以这个过程无法复制.事实证明,这些 LaTeX 包依赖项并未放入正在生成的 tex 文件中,并以错误告终.如果您关闭 RStudio 并重新启动它,当然,它的临时内存将消失,您应该能够再次加载这些包.我觉得这可能是 rmarkdown 中与全局变量相关的错误.我想我们可以通过在 render 函数的末尾添加忘记元数据"部分来修复它,但我需要查看它.

If you are curious why there is such an odd behavior, here is a short explanation. When you first load kableExtra in a rmarkdown environement, it will try to put the LaTeX package information above into the rmarkdown metadata using the usepackage_latex() function that comes with this package. It works fine if you just hit the knit button because every "knit+rendering" process is supposed to be isolated. However, surprisingly (btw thanks for bringing it up), as we see it here, if you are trying to render from console, since (my assumption) knitr or rmarkdown is trying to reuse some cached results, this process failed to replicate. It turns out these LaTeX package dependencies were not put into the tex file being generated and ends up with an error. If you close RStudio and restart it, of course, the temporary memory it has will be gone and you should be able to load those packages again. I feel like it could be a global-variable-related bug in rmarkdown. I guess we can fix it by adding a "forget the meta" part at the end of the render function but I need to look it that.

部分原因是我没有提供足够的关于过去版本中使用的 LaTeX 包的文档.现在,我在包小插图的开头添加了一个关于此问题的新部分kableExtra 0.5.0,本周早些时候发布.随意检查一下.此外,正如我之前所说,在当前的开发版本中,您可以使用函数 kableExtra_latex_packages() 调出列表.

Part of it was my bad for not providing enough documentations on LaTeX packages that were used in past releases. Now, I added a new section about this issue at the very beginning of the package vignette of kableExtra 0.5.0, which was released earlier this week. Feel free to check it out. Also, as I said earlier, in current dev version, you can bring up the list using the function kableExtra_latex_packages().

这篇关于为什么从 rmarkdown 渲染 pdf 需要在渲染之间关闭 rstudio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆