记者包可从Shiny下载docx报告 [英] Reporters package to download docx report from shiny

查看:57
本文介绍了记者包可从Shiny下载docx报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Rstudio中重现

有什么想法吗?

解决方案

请选中此行并进行调整:

  writeDoc(file = file)#将target_file替换为file 

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

1)文件名-文件将获得的名称(仅在开头进行评估,因此应将其放入反应性表达式中,以防用户输入更改).

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

否则,就像您的示例一样,您将在ShinyApp内的某个地方创建另一个.docx,而不将其指向内容函数.

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

解决方案

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.

这篇关于记者包可从Shiny下载docx报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆