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

render() fails without R session restart #1851

Closed
cawthm opened this issue Jun 26, 2020 · 5 comments
Closed

render() fails without R session restart #1851

cawthm opened this issue Jun 26, 2020 · 5 comments

Comments

@cawthm
Copy link

@cawthm cawthm commented Jun 26, 2020

Hello- I first investigated this issue on this RStudio Community post. But here's the problem:

The following toy example .Rmd knits just fine from the RStudio IDE knit button:

toy_ex.Rmd

---
title: "toy_example"
output: pdf_document
---


```{r cars}
library(kableExtra)
kable(head(mtcars), format = "latex", booktabs = T)

It fails, however, using rmarkdown::render("toy_ex.Rmd") from the console, producing this error at the end of attempted compilation:

! Undefined control sequence.
l.117 \toprule

Error: LaTeX failed to compile toy_ex.tex.

BUT if I restart R...it will successfully render via rmarkdown::render("toy_ex.Rmd"), BUT ONLY ONCE; ie a subsequent identical call will again produce the error.

Now, I realize that the use of {kableExtra} is possibly contributing to the error here, but the idiosyncratic behavior of render seems like more of an rmarkdown issue.

Thank you for your time and many contributions to this fine community.

>xfun::session_info('rmarkdown')
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4, RStudio 1.4.520

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  base64enc_0.1.3 digest_0.6.25   evaluate_0.14   glue_1.4.1      graphics_4.0.2  grDevices_4.0.2 highr_0.8       htmltools_0.5.0
  jsonlite_1.7.0  knitr_1.29      magrittr_1.5    markdown_1.1    methods_4.0.2   mime_0.9        rlang_0.4.6     rmarkdown_2.3  
  stats_4.0.2     stringi_1.4.6   stringr_1.4.0   tinytex_0.24    tools_4.0.2     utils_4.0.2     xfun_0.15       yaml_2.2.1     

Pandoc version: 2.9.2.1

By filing an issue to this repo, I promise that

  • [ x] I have fully read the issue guide at https://yihui.org/issue/.
  • [x ] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('rmarkdown'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/rmarkdown').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • [x ] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@cderv
Copy link
Collaborator

@cderv cderv commented Jun 29, 2020

Now, I realize that the use of {kableExtra} is possibly contributing to the error here, but the idiosyncratic behavior of render seems like more of an rmarkdown issue.

I think this is indeed related to KableExtra and Rmarkdown. From what I know, here is what I think is happening:

To work with Latex, kable extra requires some Tex packages to be loaded. In order to simplify, these packages are trying to be automatically "provided" by kableExtra as information for rmarkdown to be found. This happens at load time : https://github.com/haozhu233/kableExtra/blob/master/R/zzz.R
You can find about the required Tex packages in the vignette page 3: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf

It works by passing the information to knitr and rmarkdown that these latex dependencies are required. When rendering, the knit_meta information is read to see if any latex dependencies are required. If TRUE, then they are include in header before the tex file is rendered.

What happens in your case is that at first call to render() they are found and header is included, but at subsequent call they are not found, so the required Tex packages are not loaded hence the error.

In your case, you need booktabs Tex 📦 . If you load it explicitly it will work each time:

---
title: "toy_example"
output: pdf_document
header-includes:
    - \usepackage{booktabs}
---

```{r cars}
library(kableExtra)
kable(head(mtcars), format = "latex", booktabs = T)
```

It is possible that the packages are added as latex dependencies in knit_meta only for the first rendering in a clean session because it is the first loading time of kableExtra package. In the subsequent call in the same session, the kableExtra package is already loaded, so knit_meta is not modified with latex dependencies, hence the issue you encounter.
When you use the knit button, it renders in a new and fresh session each time so this is not happening.

I don't know if it is a bug, or a limitation in usage to render a Rmd file to PDF with kableExtra in a clean session each time.

@haozhu233, if I may, what do you think ?

Hope it helps.

@cawthm
Copy link
Author

@cawthm cawthm commented Jun 29, 2020

Thank you for this. Unfortunately, the addition of

 header-includes:
    - \usepackage{booktabs}

to the YAML header doesn't actually change the behavior/ solve the problem on my end, either for the toy example or my larger use case.

@cderv
Copy link
Collaborator

@cderv cderv commented Jun 29, 2020

Thanks for the feedback.

For the toy example, it should work if you copy paste the code above 🤔 It works for me with rmarkdown 2.3
this will add the missing booktab Tex packages in header in your Tex file, each time, without depending on kableExtra mechanism.

For your larger use case, with this workaround, you may need to include more that the booktabs packages. See the full list in the vignette: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf

@cawthm
Copy link
Author

@cawthm cawthm commented Jun 30, 2020

Thank you for this. I should've read the linked vignette to see the recommended req headers, which are:

header-includes:
    - \usepackage{fancyhdr}
    - \usepackage{booktabs}
    - \usepackage{longtable}
    - \usepackage{array}
    - \usepackage{multirow}
    - \usepackage{wrapfig}
    - \usepackage{float}
    - \usepackage{colortbl}
    - \usepackage{pdflscape}
    - \usepackage{tabu}
    - \usepackage{threeparttable}
    - \usepackage{threeparttablex}
    - \usepackage[normalem]{ulem}
    - \usepackage{makecell}
    - \usepackage{xcolor}

@cawthm cawthm closed this as completed Jun 30, 2020
@github-actions
Copy link

@github-actions github-actions bot commented Dec 29, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants