!未定义的控制顺序使用R / Rmarkdown / knitr

如何解决!未定义的控制顺序使用R / Rmarkdown / knitr

我是新来的,但是我在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包中的表之前。因此,我发现其他人说在rmarkdown的标头上添加options(kableExtra.latex.load_packages = FALSE)可能会有所帮助或添加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外的R控制台中重现错误。通常,这些类型的错误不是由于drake本身,而是由于drake需要您编写额外的自定义代码这一事实。在这种情况下,报表将使用RStudio IDE中的“编织”按钮运行,而不使用rmarkdown::render("report.Rmd")来运行。这使我相信问题出在报告的某处,即我们的TeX配置tinytexkablekableExtrapander。我建议将此问题重新发布为R Markdown / LaTeX问题而不是drake问题,以便您找到合适的人来帮助您。

详细说明我尝试过的事情:

  1. 当我在RStudio IDE中按下“编织”按钮时,报表将正常运行并生成外观合理的report.pdf文件。
  2. 当我在rmarkdown::render("report.Rmd")外部的R控制台中使用drake运行报告时,我得到"! Undefined control sequence. <template> ...\hskip 1sp\d@llarbegin \columncolor [HTML]{BBBBBB}\ignorespaces"
  3. 当我注释掉column_spec(2:4,background = "#BBBBBB")并再次运行rmarkdown::render("report.Rmd")时,出现一个新错误:"! Extra alignment tab has been changed to \cr. <recently read> \endtemplate

另一件事:R社区正试图淘汰iris数据集,因为它与优生学研究https://armchairecology.blog/iris-dataset/https://www.garrickadenbuie.com/blog/lets-move-on-from-iris/有联系。请尽可能避免它。 penguins数据集:https://github.com/allisonhorst/palmerpenguins是一个很好的替代品。这是您使用penguins而非iris的报表版本。

---
title: "Knitr and analysis"
author: "VJ"
output:
  pdf_document: 
    latex_engine: xelatex
tables: true
graphics: yes
---

# Example

```{r content,echo = FALSE,include=FALSE}
library(glmmTMB)
library(tinytex)
library(drake)
library(tidyverse)
library(pander)
library(kableExtra)
options(kableExtra.latex.load_packages = FALSE)
library(palmerpenguins)
```

```{r analysis qty,echo = FALSE}
fitFc <- glmmTMB(bill_length_mm ~ bill_depth_mm + species,penguins,family = gaussian)
fitFc1 <- as.data.frame(coef(summary(fitFc))$cond) 
fitFc1$variable <- rownames(fitFc1)
rownames(fitFc1) <- NULL
m1 <- fitFc1 %>%
  select(variable,everything()) %>%
  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))
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)
```
,

所以,我已经把请求放到GitHub上了,最后感谢朱Zhu,解决了它!

我所做的:

  • 删除options(kableExtra.latex.load_packages = FALSE)
  • 使用devtools::install_github("haozhu233/kableExtra")更新kableExtra软件包v1.2
  • 重新启动我的R会话
  • 运行
report = rmarkdown::render(
    #envir = new.env(),knitr_in("Reports/report.Rmd"),output_format = "pdf_document",output_file = "report.pdf") 

请注意,如果您更新软件包,则无论有无envir = new.env() ,它都可以工作

请参见此处获取原始答案:https://github.com/haozhu233/kableExtra/issues/501

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-