Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

! Undefined control sequence. with R/Rmarkdown/knitr

I'm new here, but I've spend few hours on R. I am using a combination of the R package Drake with knitr. However I have an error with the Rmarkdown file. I have a similar structure to the drake example (https://github.com/ropensci/drake), where I load data, make a graph, then a plot and a summary table of my statistical analysis (with the R package kableextra).

Everything works when I select a .tex file, with following lines on my Rmarkdown file

  output: latex_document

  tables: true
  graphics: yes 

and on drake plan

report = rmarkdown::render(
knitr_in("Reports/report.Rmd"),
output_file = "report.tex")

but when I want to have a compiled pdf file as an output with the following lines for rmarkdown header :

output:
  pdf_document: 
    latex_engine: xelatex

tables: true
graphics: yes

and drake

report = rmarkdown::render(
knitr_in("Reports/report.Rmd"),
output_format = "pdf_document",
output_file = "report.pdf")

I received an error message:

`! 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.`

So I have searched this error on the net, and go on the website of Yihui. I uninstall all TexLive from my computer, I have uninstall and re-install rmarkdown and tinytex. But it is not working and still have this error. The error occurs just before the table from kableextra package. So I've found other said that adding options(kableExtra.latex.load_packages = FALSE) could help or adding booktabs̀ and makecell on the header of rmarkdown. But it still doesn't work :-/ Any ideas or potential links that I missed?

Update reproductible example

---
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)

```

here it is details for my 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```        
like image 648
Vale Avatar asked Nov 07 '22 05:11

Vale


1 Answers

When I get questions like these, the first thing I try to do is reproduce the error in the R console outside drake. Often, these kinds of errors are not due to drake itself but instead due to the fact that drake requires you to write extra custom code. In this case, the report runs with the Knit button in the RStudio IDE, but not with rmarkdown::render("report.Rmd"). This leads me to believe the problem is somewhere in the report, our TeX configurations, tinytex, kable, kableExtra, or pander. I suggest reposting this question as an R Markdown/LaTeX issue instead of a drake issue so you can get the right people to help you.

To elaborate on what I tried:

  1. When I press the Knit button in the RStudio IDE, the report runs normally and produced a reasonable-looking report.pdf file.
  2. When I run the report using rmarkdown::render("report.Rmd") in the R console outside drake, I get "! Undefined control sequence. <template> ...\hskip 1sp\d@llarbegin \columncolor [HTML]{BBBBBB}\ignorespaces".
  3. When I comment out column_spec(2:4,background = "#BBBBBB") and run rmarkdown::render("report.Rmd") again, I get a new error: "! Extra alignment tab has been changed to \cr. <recently read> \endtemplate.

Another thing: the R community is trying to retire the iris dataset because of its connection to eugenics research: https://armchairecology.blog/iris-dataset/, https://www.garrickadenbuie.com/blog/lets-move-on-from-iris/. Please avoid it whenever possible. A great drop-in replacement is the penguins dataset: https://github.com/allisonhorst/palmerpenguins. Here is a version of your report that uses penguins instead of 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)
```
like image 123
landau Avatar answered Nov 20 '22 17:11

landau