四处寻找我的问题的答案,但我很困惑。
我正在尝试在脚本中使用 rmarkdown::render 批量渲染一些 pdf,但它一直给我这个错误:
! Use of \@array doesn't match its definition.
\new@ifnextchar ...served@d = #1\def \reserved@a {
#2}\def \reserved@b {#3}\f...
l.85 ...2in}|>{\raggedright\arraybackslash}p{4in}}
Error: Failed to compile C:/Users/cmykim/Desktop/stackoverflow/testfile.tex.
See testfile.log for more info.
In addition: Warning messages:
1: running command '"pdflatex" -halt-on-error -interaction=batchmode
"C:/Users/cmykim/Desktop/stackoverflow/testfile.tex"' had status 1
2: running command '"pdflatex" -halt-on-error -interaction=batchmode
"C:/Users/cmykim/Desktop/stackoverflow/testfile.tex"' had status 1
但是,当我运行 .Rmd 文件本身时,它会毫无问题地生成 PDF。
我创建了一个示例,但出现以下错误:
rmarkdown 文件:
---
output: pdf_document
---
```{r setup, include=FALSE}
## Packages
library(tidyverse)
library(knitr)
library(kableExtra)
library(tinytex)
dat <- data.frame(stringsAsFactors=FALSE,
id = c(1L, 2L, 3L),
name = c("bob", "sam", "jane"),
question1 = c("Lorem ipsum dolor sit amet", "consectetur adipiscing",
"Integer quis")
)
dat <- dat %>% slice(1) %>% gather("key", "value")
```
```{r echo=FALSE}
dat %>%
kable("latex", col.names = NULL) %>%
# making column 2 4 inches wide
column_spec(2, width = "4in") %>%
# making column 1 2 inches wide
column_spec(1, width = "2in")
r 脚本:
library(rmarkdown)
rmarkdown::render(input =
"C:\\Users\\cmykim\\Desktop\\stackoverflow\\reproducible.Rmd",
output_format = "pdf_document",
output_file = "testfile.pdf",
output_dir = "C:\\Users\\cmykim\\Desktop\\stackoverflow")
我安装了tinytex,希望它能修复它,但它没有。
如有任何帮助,我们将不胜感激!
在我的机器上,我将以下内容添加到 YAML 命令中并且它有效:
---
header-includes:
\usepackage{array}
output:
pdf_document
---
我找到了解决方案这里。
显然,当您渲染 Markdown 文件时,您将所有内容存储在新环境中,而 knitting 则适用于当前(已经存在)的环境。因此,在使用
rmarkdown::render()
时,并非所有函数或对环境中现有项目的调用都可以正常工作,而它们在控制台或编织时却可以正常工作。
解决方案是在
rmarkdown::render()
函数中指定渲染时使用哪个环境。 envir = parent.frame()
对于桅杆实例来说应该可以正常工作。
rmarkdown::render(YourMarkdownFile,
envir = parent.frame(),
output_file = "YourOutputFile.pdf")