0

我最近更新了我的操作系统、软件包、RStudio 和 R,并尝试运行一个在所有更新之前运行良好的 .Rmd 文件。当我运行 .Rmd 时,在尝试渲染 PDF 文档(见下文)时,最后(达到 100% 之后)出现错误。在逐个分解并运行我的 Rmarkdown 文件后,我发现问题出在scalebox = 我用来生成带有texreg. 我很高兴我发现了这个问题,但我很好奇为什么 scalebox 不再在 Rmarkdown 文档中工作了。下面的 Reprex (如果你删除scalebox = .75,它会渲染得很好)。有什么想法吗?

title: "Reprex"
author: "Author"
date: ""
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(texreg)

df <- data.frame(y = rnorm(100),
                 x = rnorm(100))

model <- lm(y ~ x, data = df)


```{r, results='asis'}
texreg(model,
       scalebox = .75)
output file: Reprex.knit.md

! LaTeX Error: Can be used only in preamble.

Error: LaTeX failed to compile Reprex.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Reprex.log for more info.
Execution halted
4

1 回答 1

1

要使用scalebox = 0.75texreg需要使用graphicx包。它没有设置为与 一起使用knitr,因此它只是\usepackage{graphicx}在表格前面输出命令,这是非法的。我想您应该将输出剪切并粘贴到您的文档中,该行进入序言而不是正文。

要解决此设计,只需将use.packages = FALSE调用设置为texreg(). 既然knitr已经使用了graphicx,那就足够了。

如果您在某些knitr不包含的包中遇到相同的错误(也许您使用siunitx = TRUE了 ,它需要该siunitx包),那么您需要显示结果以确定它需要哪个包,然后将其添加到 YAML文件,例如

texreg(model,
       scalebox = .75, siunitx = TRUE)

\usepackage{graphicx}
\usepackage{siunitx}
...

它告诉您在运行之前将其添加到 YAML 中use.packages = FALSE

output: 
  pdf_document:
    extra_dependencies: siunitx

然后代码块将更改为

texreg(model,
       scalebox = .75, siunitx = TRUE, use.packages = FALSE)
于 2020-09-23T18:15:14.967 回答