0

I'm attempting to export a kable table (latex) to a .pdf file using R Shiny.

Here is the code I am using to generate the kable latex table:

x<-kable(overall, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T,
             caption = "Sample Table",  
             escape = FALSE)%>%
      kable_styling(latex_options = c("striped")) %>%
      add_header_above(c(" " = 1, 'Group 1' = 3, 'Group 2' = 3))%>%
      landscape()

save_kable(x, 'SampleTable.pdf')

I'm able to export this in a standalone R program, but I'd like to replicate the export with R Shiny. I attempted to wrap the code in the downloadHandler function, but it doesn't work.

Sample code:

output$export = downloadHandler(
    filename = function() {"sampleTable.pdf"},
    content = function(file) {
     x<-kable(overall, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T,
         caption = "Sample Table",  
         escape = FALSE)%>%
  kable_styling(latex_options = c("striped")) %>%
  add_header_above(c(" " = 1, 'Group 1' = 3, 'Group 2' = 3))%>%
  landscape()

  save_kable(x, file)
    }
  )

Any insight would be appreciated.

statsguyz
  • 419
  • 2
  • 11
  • 35
  • When I save a pdf file this way, I call `grDevices::pdf(file); plot(x); grDevices::dev.off()` within the file function. It probably works the same with `save_kable`, so try to replace the returned `x` with `save_kable(x, file)`. – user12728748 Mar 28 '20 at 17:52
  • This does not appear to work. – statsguyz Mar 28 '20 at 19:51

1 Answers1

1

This works for me (after installing pandoc and texlive-xetex):


library(shiny)
library(knitr)
library(kableExtra)

library(datasets)
options(knitr.table.format = "latex") # not required in newer versions of kableExtra

server <- function(input, output) {

    # Fill in the spot we created for a plot
    output$phonePlot <- renderPlot({

        # Render a barplot
        barplot(WorldPhones[,input$region]*1000, 
                main=input$region,
                ylab="Number of Telephones",
                xlab="Year")
    })
    output$export = downloadHandler(
        filename = function() {"sampleTable.pdf"},
        content = function(file) {
            x <- kable(WorldPhones, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T,
                     caption = "Sample Table",  
                     escape = FALSE) %>%
                kable_styling(latex_options = c("striped")) %>%
                add_header_above(c(" " = 1, 'Group 1' = 3, 'Group 2' = 3)) %>%
                landscape()

            save_kable(x, file)
        },
        contentType = 'application/pdf'
    )

}

ui <- fluidPage(    

    # Give the page a title
    titlePanel("Telephones by region"),

    # Generate a row with a sidebar
    sidebarLayout(      

        # Define the sidebar with one input
        sidebarPanel(
            selectInput("region", "Region:", 
                        choices=colnames(WorldPhones)),
            hr(),
            helpText("Data from AT&T (1961) The World's Telephones."), 
            shiny::downloadButton("export", "Export")
        ),

        # Create a spot for the barplot
        mainPanel(
            plotOutput("phonePlot")  
        )

    )
)

shinyApp(ui = ui, server = server)

user12728748
  • 8,106
  • 2
  • 9
  • 14
  • I'm unable to get it to work. I get the following message when I click 'Export': This is XeTeX, Version 3.14159265-2.6-0.99999 (MiKTeX 2.9.6930 64-bit) entering extended mode. I tried to save the file but nothing is written. – statsguyz Mar 28 '20 at 21:05
  • Maybe this is because you don't have the MiKTeX version installed on your computer. Try downloading and install it from here (https://miktex.org/download) and then rerun the code. It should work. – Miha Mar 28 '20 at 21:24
  • I have downloaded the latest version of MikTex. Unfortunately this has not solved the problem. – statsguyz Mar 28 '20 at 22:08
  • It is probably a MiKTeX issue. See https://stackoverflow.com/questions/50082865/miktex-did-not-succeed-after-i-updated-r-to-version-3-5-0 (Yihui's comment) and https://yihui.org/en/2018/03/miktex-auto-install/, and enable automatic installation of missing packages. – user12728748 Mar 28 '20 at 22:36
  • I have MiKTeX set to automatically install packages if they are missing. The PDF generates if I used a stand-alone R program using save_kable. Unfortunately it still doesn't work in the R Shiny app. – statsguyz Mar 29 '20 at 00:28
  • My code works when I use the given answer. I'll have to see how I can get my data frame 'overall' in the same format as 'WorldPhones'. – statsguyz Mar 29 '20 at 00:33
  • It looks like I'll need to figure out how to use a data frame that is created in the reactive environment. The data used here is imported from the 'datasets' package. – statsguyz Mar 29 '20 at 00:56
  • Finally worked. Had to restart my computer and move all of the code that generates the data frame inside the content = function (file) { . – statsguyz Mar 29 '20 at 17:40