代码之家  ›  专栏  ›  技术社区  ›  Ali Hadjihoseini

从shiny下载docx报告的Reporters包

  •  1
  • Ali Hadjihoseini  · 技术社区  · 8 年前

    我试图复制 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)
    

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  3
  •   Christian    8 年前

    请检查该行并进行调整:

    writeDoc(file = file) #replace target_file with file
    

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

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

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