R Shiny downloadHandler()什么都不保存

时间:2016-02-23 18:09:02

标签: r shiny xlsx

在ui.R中没有什么是不同的

    shinyUI(fluidPage(
      downloadButton('downloadData','Save Results')
    )
)

在server.R

output$downloadData <- downloadHandler(

  filename = function () { paste0("output", ".xlsx", sep='') },
   content = function (file) { 
  fname <- paste(file,"xlsx",sep=".")
  write.xlsx2(Correlation.df,fname ,"Correlation",row.names = FALSE)
  write.xlsx2(Covariance.df, fname, "Covariance",row.names = FALSE,append = TRUE)
  file.rename(fname, file)
                              }
                          )

问题是可以使用正确的文件名启动保存窗口,但不保存任何内容。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您的downloadHandler未提供content功能。这对我有用:

library(shiny)
library(xlsx)

app <- shinyApp(
  ui = fluidPage(
    downloadLink('downloadData', 'Download')
  ),

  server = function(input, output) {
    df1 <- data.frame(
      A = 1:5, Source = "df1",
      stringsAsFactors = FALSE
    )
    df2 <- data.frame(
      A = 6:10, Source = "df2",
      stringsAsFactors = FALSE
    )

    output$downloadData <- downloadHandler(
      filename = function(file) {
        paste0("samplefile", ".xlsx")
      },
      content = function(con) {
        write.xlsx2(df1, con, sheetName = "df1", row.names = FALSE)
        write.xlsx2(df2, con, sheetName = "df2", row.names = FALSE, append = TRUE)
      }
    )
  }
)

runApp(app)