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)
}
)
问题是可以使用正确的文件名启动保存窗口,但不保存任何内容。
提前致谢。
答案 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)