如何在 zip 文件中下载使用 R markdown 和 R shiny 创建的多个报告

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

我创建了一个 R shiny 应用程序来使用 R Markdown 下载动态报告。以前我通过 selecting r shiny 中数据 table 中的行并单击下载按钮一次下载一份报告,selected 行的列值将填充到报告,这工作得很好。

但现在我正在尝试下载多份报告,因此如果我 select 数据中的多行 table in r shiny 然后点击下载,下载的报告数量应该相等到行数 selected。 为此,我正在尝试创建一个包含我所有个人报告的 zip 文件,但我收到的是

错误:pandoc 文档转换失败,错误 1

我已针对此错误进行了研究,但没有找到任何信息。请帮忙!

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

这是一个可重现的示例(要使其正常工作,请使用 RStudio 创建一个包含默认内容的 rmarkdown 文件,并将其另存为 "test.rmd" 并保存在与您的 Shiny 应用相同的文件夹中)。

重要:

  • 您需要 运行 在您的网络浏览器中从外部访问该应用程序。不知何故,它在查看器窗格或 RStudio window 中不起作用(您下载了 window 但没有文件被保存)。
  • 如果你在Windows,你需要确保先安装RTools,并将rtools/bin文件夹放在你的系统路径中。

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)

您好,我还安装了 Rtools/bin 并且是 运行 网络浏览器上的代码,但是当我点击下载按钮时,下载 window 没有出现并显示 ' 404 Not Found',但是当我检查目录时,doc 文件报告直接保存到目录,没有生成 zip 文件。请看下面的代码。

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))`