Radhikesh Ranadive Radhikesh Ranadive - 1 month ago 63
R Question

How to download multiple reports created using R markdown and R shiny in a zip file

I have created an R shiny application to download dynamic reports using R Markdown. Previously I was downloading one report at a time by selecting the row in the data table in r shiny and clicking on download button, the selected row's column values would get filled in the report, this was working perfectly fine.

But now i am trying to download multiple reports, so that if I select multiple rows in a datatable in r shiny and click on download, the number of reports downloaded should be equal to number of rows selected.
For this I am trying to create a zip file which contains all my individual report but I am getting this

error: pandoc document conversion failed with error 1

I had researched for this error but couldn't find anything. Please help!

ui <- {
tagList(
div(id = "downloadBtn",
downloadButton("downloadData", "Download")),
DT::dataTableOutput('myTable1')
)
}

dataJ <- read.csv(file = "iris.csv", header = TRUE, stringsAsFactors =
FALSE)


server <- function(input, output)
{
output$myTable1 <- DT::renderDataTable({
DT::datatable(dataJ, options = list(orderClasses = TRUE), filter = 'top')})

output$downloadData <- downloadHandler(

filename = function()
{
paste("output", "zip", sep = ".")
},
content = function(file)
{
k = list(input$myTable1_rows_selected)
fs <- c()
for ( i in k)
{
params <- list(j=i)
path <- paste(i,".docx")

rmarkdown::render("R_markdown_script.Rmd", rmarkdown::word_document(),
output_file = path , params = params,
envir = new.env(parent = globalenv()))

fs <- c(fs,path)
}
zip(zipfile = file, files = fs)
if (file.exists(paste0(file, ".zip")))
file.rename(paste0(file, ".zip"), file)
},
contentType = "application/zip" )
}



runApp(list(ui = ui, server = server))

Answer

Here is a reproducible example (to make it work, create an rmarkdown file with the default content using RStudio, and save it as "test.rmd" in the same folder as your Shiny app).

Important:

  • You need to run the app externally inside your web browser. Somehow it does not work in the viewer pane or RStudio window (you get the download window but then no file is saved).
  • If you are on Windows, you need to make sure that you install RTools first, and also put the rtools/bin folder in your system path.

app.R

library(shiny)

ui <- shinyUI(fluidPage(

  titlePanel("Old Faithful Geyser Data"),

  sidebarLayout(
    sidebarPanel(
      downloadButton("downloadData", "Download")
    ),

    mainPanel(
      DT::dataTableOutput('myTable1')
    )
  )
))

server <- shinyServer(function(input, output) {

  output$myTable1 <- DT::renderDataTable(iris)

  output$downloadData <- downloadHandler(
    filename = function() {
      paste0("output", ".zip")
    },
    content = function(file) {
      k <- input$myTable1_rows_selected
      fs <- c()
      for (i in k) {
        path <- paste0(i, ".docx")
        rmarkdown::render("test.rmd", rmarkdown::word_document(), output_file = path)
        fs <- c(fs, path)
      }
      zip(file, fs)
    },
    contentType = "application/zip"
  )

})

shinyApp(ui = ui, server = server)
Comments