2
votes

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))
2
Since it's a pandoc error, it's related to the rmarkdown:render line and not related to the zip file creation. - Xiongbing Jin
Thank you for the reply, I changed the output_file argument to file in rmarkdown::render line, now the zip file is created but the word reports are not generated. The compressed zip folder has _rels, docProps and word folder which have some xml files. Still not able to generate the reports. Can you please suggest how can I generate the zip file with word reports in it. I changed the rmarkdown::render line as below..... rmarkdown::render("R_markdown_script.Rmd", rmarkdown::word_document(), output_file = file , params = params, envir = new.env(parent = globalenv())) - radhikesh93
When you change output_file=file in rmarkdown, you are in fact rendering rmarkdown file directly to your final downloaded zip file. Since docx is basically a zip file, what you saw is the internal structure of a docx file. - Xiongbing Jin

2 Answers

1
votes

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)
0
votes

Hello I also installed Rtools/bin and was running the code on the web browser, but when I click on download button, download window doesn't comes up and shows '404 Not Found', but when I check the directory, the doc files report are saving directly to directory, no zip file is produced. Please see below code.

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 = ("output.zip"),


content = function(file) 
{

k <- (input$myTable1_rows_selected)
fs <- c()
for ( i in k) 
  {
  path <- paste0(i,".docx")
  rmarkdown::render("R_markdown_script.Rmd", output_file = path , 
  params = list(j=i), envir = new.env(parent = globalenv()))

  fs <- c(fs,file)
  }
zip(zipfile = file, files = fs)

  }, 
  contentType = "application/zip" )
}

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