rshinyr-markdown

How to run rmd file from shiny


I am having problems with shiny app executing my rmd file. Shiny successfully runs my rmd file when I first open Rstudio but it does not run my rmd file a second time. Instead, I get the below error message after hanging for a while:

tlmgr.pl install: package already present: l3backend
tlmgr.pl install: package already present: l3backend-dev
! Undefined control sequence.
l.128 \toprule

Warning: Error in : LaTeX failed to compile KableTestTable.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See KableTestTable.log for more info.
  1: runApp

For example, if I run the shiny app and from the window and I choose pdf as the output format, it runs fine and executes the first time I open Rstudio but I get the error message if I try to run it again as any format. Below is a short version of my shiny app and rmd file. Any help is appreciated.

 #Shiny app
ui <- fluidPage(
  fluidRow(column(width = 1, "")),column(width = 4, ""), 
  title = 'Download a PDF report',
    sidebarPanel(
      radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                   inline = TRUE),
      downloadButton('downloadReport')
    )
    )

server <- function(input, output, session) {
  session$onSessionEnded(function() {
    stopApp()
  })

  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('Kable-Test', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },
    
    content = function(file) {

      library(rmarkdown)
      out <- render('KableTestTable.Rmd', switch(
        input$format,
        PDF = pdf_document(), 
        HTML = html_document(), 
        Word = word_document()))
      file.rename(out, file)
    }
  )
  }
shinyApp(ui, server)

And here is the rmd file that I am attempting to run from the app. I have to mention that the rmd file runs fine everytime if I run it from Rstudio and only crashes with the app.

---
title: "Kable Test"
header-includes: \usepackage{pdflscape} \usepackage{caption} \usepackage[T1]{fontenc} 
geometry: "left=2cm,right=2cm,top=2cm,bottom=2cm"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. 

```{r,echo=FALSE}
library(knitr)
library(kableExtra)
```

```{r}
kable(head(mtcars,15), format = "latex",booktabs=TRUE,linesep="")
```

Solution

  • I have no clue why the Rmd renders fine when hitting the Knit button but fails with render(). But one option to fix your issue would be to add the booktabs package, i.e. add \usepackage{booktabs} to header-includes: in your YAML.

    ---
    title: "Kable Test"
    header-includes:
      - \usepackage{pdflscape}
      - \usepackage{caption}
      - \usepackage\[T1\]{fontenc}
      - \usepackage{booktabs}
    geometry: "left=2cm,right=2cm,top=2cm,bottom=2cm"
    output:
    pdf_document: default
    html_document:
      df_print: paged
    
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.
    
    ```{r,echo=FALSE}
    library(knitr)
    library(kableExtra)
    ```
    
    ```{r}
    kable(head(mtcars, 15), format = "latex", booktabs = TRUE, linesep = "")
    ```