Reporters package to download docx report from shiny

為{幸葍}努か 提交于 2019-12-11 08:08:24

问题


I tried to reproduce This example in Rstudio, It is working very well. Then I just put everything together to download the template in shiny ! but it does not work :

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)

if I run the server content, without putting in shiny, It would update my template but in shiny when I click on download button, it returns :

Any idea where is the mistake ???


回答1:


Please check this line and adapt:

writeDoc(file = file) #replace target_file with file

Why? The function DownloadHandler takes two main arguments:

1) filename - the name the file will get (evaluated only at the beginning, so put it inside a reactive expression in case it changes by user input).

2) content - takes already care of creating a temporary file for you, hence you need to supply the file argument from your content function.

Otherwise (like in your example) you create a second .docx somewhere inside ShinyApp without pointing it to the content function.



来源:https://stackoverflow.com/questions/46281137/reporters-package-to-download-docx-report-from-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!