【发布时间】:2023-03-05 02:56:02
【问题描述】:
我是新来的,但我在 R 上花了几个小时。我正在使用 R 包 Drake 和 knitr 的组合。 但是我对 Rmarkdown 文件有一个错误。我的结构与 drake 示例 (https://github.com/ropensci/drake) 类似,我在其中加载数据,制作图表,然后绘制图表和统计分析的汇总表(使用 R 包 kableextra)。
当我选择一个 .tex 文件时一切正常,在我的 Rmarkdown 文件中有以下几行
output: latex_document
tables: true
graphics: yes
并在德雷克计划中
report = rmarkdown::render(
knitr_in("Reports/report.Rmd"),
output_file = "report.tex")
但是当我想将编译后的 pdf 文件作为 rmarkdown 标头的以下行作为输出时:
output:
pdf_document:
latex_engine: xelatex
tables: true
graphics: yes
和德雷克
report = rmarkdown::render(
knitr_in("Reports/report.Rmd"),
output_format = "pdf_document",
output_file = "report.pdf")
我收到一条错误消息:
`! Undefined control sequence.
<template> ...\hskip 1sp\d@llarbegin \columncolor
[HTML]{BBBBBB}\ignorespaces
l.178 \em{Predictors} & \em
{Coefficients} & \em{SE} & \em{P-value} & \em{Coe...
x fail report
Erreur : target report failed.
diagnose(report)$error$message:
LaTeX failed to compile report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See report.log for more info.`
所以我在网上搜索了这个错误,然后上亿辉的网站。我从我的计算机上卸载了所有 TexLive,我已经卸载并重新安装了 rmarkdown 和 tinytex。但它不工作,仍然有这个错误。
错误发生在 kableextra 包中的表之前。所以我发现其他人说添加options(kableExtra.latex.load_packages = FALSE) 可能会有所帮助,或者在rmarkdown 的标题上添加booktabs̀ 和makecell。但它仍然不起作用:-/我错过了任何想法或潜在链接?
更新可复制示例
---
title: "Knitr and analysis"
author: "VJ"
output:
pdf_document:
latex_engine: xelatex
tables: true
graphics: yes
---
# Example
```{r content, echo = FALSE, include=FALSE}
#tinytex::install_tinytex()
# output:
# pdf_document:
# latex_engine: xelatex
#pdf_document or latex_document
#latex_engine: xelatex
library(glmmTMB)
library(tinytex)
library(drake)
library(tidyverse)
library(pander)
#options(kableExtra.latex.load_packages = FALSE)
library(kableExtra)
options(kableExtra.latex.load_packages = FALSE)
data("iris")
```
```{r analysis qty, echo = FALSE}
#outputs table from four model
fitFc = glmmTMB(Sepal.Width ~ Petal.Length +
Species,
iris,
family = gaussian)
fitFc1 <- as.data.frame(coef(summary(fitFc))$cond)
varname1 <- c("Intercept", "PL", "Plant 1 - Versicolor", "Plant 2 - Virginica")
fitFc1 <- cbind(varname1,fitFc1)
rownames(fitFc1) <- NULL
m1 <- fitFc1 %>% select(1,2,3,5) %>%
rename("Predictors" = 1,
"Coefficients" = 2,
"SE" = 3,
"pvalue" = 4) %>%
mutate(pvalue = format.pval(pvalue, eps = .001, digits = 1)) %>%
mutate_at(vars(-pvalue, -Predictors), ~round(., 2))
#bold significant pvalue and then create output summary from glmm
m1$pvalue<- cell_spec(m1$pvalue, bold = ifelse(m1$pvalue< 0.05, TRUE, FALSE))
kable(m1, format = "latex", booktabs = T, caption = "Summary of linear model (LMMs).",
col.names = c("Predictors",
"Coefficients",
"SE",
"P-value"), escape = F) %>%
kable_styling(latex_options = c("hold_position")) %>%
column_spec(2:4,background = "#BBBBBB") %>%
kable_styling() %>%
add_header_above(c(" " = 1, "LMM1" = 3), bold = T) %>%
row_spec(0, bold = FALSE, italic = T)
```
这里是我的 sessionInfo() 的详细信息
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=fr_FR.UTF-8 LC_NUMERIC=C LC_TIME=fr_FR.UTF-8
[4] LC_COLLATE=fr_FR.UTF-8 LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=fr_FR.UTF-8
[7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] kableExtra_1.1.0 tinytex_0.25 pander_0.6.3 sjstats_0.18.0 sjPlot_2.8.4
[6] glmmTMB_1.0.2.1 lmerTest_3.1-2 lme4_1.1-23 Matrix_1.2-18 ggeffects_0.15.1
[11] effects_4.2-0 carData_3.0-4 cowplot_1.0.0 forcats_0.5.0 stringr_1.4.0
[16] dplyr_1.0.0 readr_1.3.1 tidyr_1.1.0 tibble_3.0.1 ggplot2_3.3.2
[21] tidyverse_1.3.0 drake_7.12.4 purrr_0.3.4
loaded via a namespace (and not attached):
[1] minqa_1.2.4 colorspace_1.4-1 ellipsis_0.3.1 sjlabelled_1.1.6
[5] estimability_1.3 parameters_0.8.2 fs_1.4.2 rstudioapi_0.11
[9] farver_2.0.3 fansi_0.4.1 mvtnorm_1.1-1 lubridate_1.7.9
[13] xml2_1.3.2 splines_4.0.2 knitr_1.29 sjmisc_2.8.5
[17] jsonlite_1.7.0 nloptr_1.2.2.2 broom_0.5.6 dbplyr_1.4.4
[21] effectsize_0.3.2 compiler_4.0.2 httr_1.4.1 emmeans_1.4.8
[25] backports_1.1.8 assertthat_0.2.1 survey_4.0 cli_2.0.2
[29] htmltools_0.5.0 prettyunits_1.1.1 tools_4.0.2 igraph_1.2.5
[33] coda_0.19-3 gtable_0.3.0 glue_1.4.1 Rcpp_1.0.5
[37] cellranger_1.1.0 vctrs_0.3.1 nlme_3.1-147 insight_0.9.0
[41] xfun_0.16 rvest_0.3.5 lifecycle_0.2.0 statmod_1.4.34
[45] MASS_7.3-51.6 scales_1.1.1 hms_0.5.3 parallel_4.0.2
[49] TMB_1.7.18 RColorBrewer_1.1-2 yaml_2.2.1 stringi_1.4.6
[53] bayestestR_0.7.2 filelock_1.0.2 boot_1.3-25 storr_1.2.1
[57] rlang_0.4.7 pkgconfig_2.0.3 evaluate_0.14 lattice_0.20-41
[61] labeling_0.3 tidyselect_1.1.0 magrittr_1.5 R6_2.4.1
[65] generics_0.0.2 base64url_1.4 txtq_0.2.3 DBI_1.1.0
[69] mgcv_1.8-31 pillar_1.4.4 haven_2.3.1 withr_2.2.0
[73] survival_3.2-3 nnet_7.3-14 performance_0.4.8 modelr_0.1.8
[77] crayon_1.3.4 utf8_1.1.4 rmarkdown_2.3.3 progress_1.2.2
[81] grid_4.0.2 readxl_1.3.1 blob_1.2.1 reprex_0.3.0
[85] digest_0.6.25 webshot_0.5.2 xtable_1.8-4 numDeriv_2016.8-1.1
[89] munsell_0.5.0 viridisLite_0.3.0 mitools_2.4```
【问题讨论】:
-
如果您在
drake之外运行报告,是否也会遇到同样的错误? -
另外,查看您正在使用的完整
Reports/report.Rmd文件会有所帮助。相关:stackoverflow.com/questions/5963269/… -
我已经更新了我的请求。不知道为什么,但我无法添加“块”代码,它就在“#example”之后直到结束
-
谢谢,这有助于缩小范围。我还格式化了 R Markdown 源代码(缩进 4 个空格)并删除了
drake-r-package标签,因为错误似乎不是来自drake。 -
报告在我这边编译得很好,所以我不确定我是帮助解决 LaTeX 问题的最佳人选,但我认为向其他人提供您的系统信息会有所帮助:操作系统、
sessionInfo()、LaTeX 的版本等。如果您不手动设置 LaTeX 引擎,您可能还会看到会发生什么。当我删除xelatex时,报告仍然运行良好。
标签: latex r-markdown knitr kableextra pander