Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

! Undefined control sequence #501

Closed
ValentinJourne opened this issue Aug 29, 2020 · 5 comments
Closed

! Undefined control sequence #501

ValentinJourne opened this issue Aug 29, 2020 · 5 comments

Comments

@ValentinJourne
Copy link

Hi,
I am using a combination of knitr with kableextra. I wanted to export a summary table from my statistical analysis but I have an error when I want to compile my report (in rmd) into a pdf with the function rmarkdown::render. But it is working when I use the knit button on Rstudio.

I have copy/paste a reproductible example below. My question was also on stackoverflow, because I thought at the beginning that the error come from another package (which is not true). My question is also here: https://stackoverflow.com/questions/63580502/undefined-control-sequence-with-r-rmarkdown-knitr

I am using Ubuntu v18. Details of my package are also reported on the stack link. But I am maybe not the only one to have this error. When I delete the table, the function is working (rmarkdown::render) so I think that the error come from the table created with kableextra. the function used is

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

Do I forget something? An example of report rmd is just after

---
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)
```
@haozhu233
Copy link
Owner

See https://stackoverflow.com/questions/46080853/why-does-rendering-a-pdf-from-rmarkdown-require-closing-rstudio-between-renders/46083308#46083308

I think by adding envir = new.nev() to your render function will solve this issue.

Another possible way of solving this is to use the new kbl function instead of kable as it adds a in-function format checking every time you call it.

@ValentinJourne
Copy link
Author

ValentinJourne commented Aug 29, 2020

I still have the error if I use the following function

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

I have a look on your pdf : https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

But if I wrote kbl instead of kable, R did not find this function. But this is weird because my version of kableExtra is kableExtra_1.1.0

@haozhu233
Copy link
Owner

Yeah, kbl is in 1.2.1

@haozhu233
Copy link
Owner

Also, why do you have options(kableExtra.latex.load_packages = FALSE) while you don't have all the required latex packages? I don't think you can even render a document by hitting the knit button.

@ValentinJourne
Copy link
Author

I do not remember, I think I've added it at the beginning as a potential solution, but now I deleted it. Not also that I did not change the function kable by kbl each time in my reports.

I've updated your package (now v1.2.1), and tested it again with the same function. And now it works, even with or without envir = new.env() ! :D

But in my case, I do not know if it is because I specify kable_styling(latex_options = c("hold_position")) in my report, I added in the YAML header to avoid this error ( ! LaTeX Error: Unknown float option `H'.)

header-includes:
   - \usepackage{float}

Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants