使用ReporteRs包使用R Shiny生成单词报告

问题描述 投票:0回答:1

我有一个Shiny代码来上传.csv文件并从该文件中选择几个列。

我想将数据保存为.docx文件,只需单击下载按钮即可。

到目前为止,HTML是由下载按钮生成的。

Code:

# Server 

server = function(input, output, session) {

 inFile <- reactive({input$datasetA})

  data_df <- reactive({

  if(is.null(inFile()))

  return(NULL)

read.csv(inFile()$datapath, header = T, sep = ',', 
         stringsAsFactors = F)
}) 

output$downloadData  <- downloadHandler(

filename=paste0("Data_", input$selectA, input$selectB, "_", Sys.Date(), ".docx"),

content = function(file) {

  doc <- docx()
  data<- data_df()[1:10, 1:10]
  # Add a first table : Default table
  doc <- addTitle(doc, "Default table")
  doc <- addFlexTable( doc, FlexTable(data))
  doc <- addParagraph(doc, c("", "")) # 2 line breaks
  # Add a second table, theme : vanilla table
  doc <- addTitle(doc, "Vanilla table")
  doc <- addFlexTable( doc, vanilla.table(data))
  writeDoc(doc, file = "r-reporters-word-document-add-table.docx")

})

}

shinyApp(ui = ui, server = server)

有人可以指出我的错误吗?

r shiny
1个回答
1
投票

downloadHandler的文档可以在这里阅读:https://shiny.rstudio.com/reference/shiny/0.14/downloadHandler.html

filename()content()应该是功能:

library(shiny)
library(ReporteRs)

ui <- fluidPage(
  downloadButton('downloadpptx', 'Download')
)

server <- function(input, output) {

  output$downloadpptx <- downloadHandler(
    filename = function() {
      "example.docx"
    },
    content = function(file) {
      doc <- docx()
      doc <- addFlexTable( doc, vanilla.table(head(iris)))
      writeDoc(doc, file = file)
    }
  )

}

shinyApp(ui = ui, server = server)
© www.soinside.com 2019 - 2024. All rights reserved.