问题描述
我最近更新了我的OS,软件包,RStudio和R,并尝试运行一个.Rmd文件,该文件在所有更新之前均能正常工作。当我运行.Rmd时,尝试渲染PDF文档时(在下面看到),最终(在达到100%之后)出现错误。分解并逐个运行Rmarkdown文件后,我发现问题是我用来用scaleBox =
生成表的texreg
参数。我很高兴发现了这个问题,但是很好奇为什么scaleBox在Rmarkdown文档中不再起作用。在下面重新表示(如果删除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
解决方法
要使用scalebox = 0.75
,texreg
需要使用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}
...
告诉您在使用use.packages = FALSE
运行之前将其添加到YAML:
output:
pdf_document:
extra_dependencies: siunitx
,然后将代码块更改为
texreg(model,siunitx = TRUE,use.packages = FALSE)