从 shiny 下载 docx 报告的记者包

Reporters package to download docx report from shiny

我尝试在 Rstudio 中重现 This example,效果很好。然后我就把所有东西放在一起下载闪亮的模板!但它不起作用:

library(shiny)
library(ReporteRs)
library(ReporteRsjars)
library( ggplot2 )
library( magrittr )
library( ggplot2 )
library( magrittr )

ui<-  fluidPage(    

  downloadButton('downloadData', 'Download')

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

  output$downloadData <- downloadHandler(
    filename = "file.docx",

    content = function(file) {

      target_file <- "bookmark_example.docx" # file to produce 
      template <- system.file(package = "ReporteRs", 
                              "templates/bookmark_example.docx" ) # template example

      doc = docx(template=template)

      ft <- vanilla.table( data = head(iris), add.rownames=TRUE )

      myplot1 <- ggplot(data = iris, aes(Sepal.Length, Petal.Length, color = Species), 
                        alpha = I(0.7) )


      doc %>%
        addParagraph( value = "Jane Doe", stylename = "small", bookmark = "AUTHOR" ) %>%
        addParagraph( value = "John Doe", stylename = "small", bookmark = "REVIEWER" ) %>%
        addFlexTable( flextable = ft, bookmark = "DATA" ) %>%
        addPlot( fun = print, x = myplot1, bookmark = "PLOT" ) %>%
        writeDoc( file = target_file)

    }
  )
}

shinyApp(ui=ui,server=server)

如果我 运行 服务器内容,没有放入 shiny,它会更新我的模板,但是当我点击下载按钮时,它是 shiny,它 returns :

知道错在哪里吗???

请检查此行并修改:

writeDoc(file = file) #replace target_file with file

为什么? DownloadHandler 函数有两个主要参数:

1) filename - 文件将获得的名称(仅在开始时评估,因此将其放在反应式表达式中以防它因用户输入而更改)。

2) content - 已经为您创建了一个临时文件,因此您需要从 content 函数中提供文件参数。

否则(就像你的例子一样)你在 ShinyApp 的某个地方创建了第二个 .docx 而没有将它指向内容函数。