I am working with R (shiny) and want to save a dataframe as an excel file. For this purpose I use the \"shinyFiles\" package so that the user can specify where the excel file is
Here is a working example. Again, this assumes that you run the app on your own computer, and users are allowed to access folders on this computer. You can set the root folder where user is allowed to save files (see UserFolder
, user will be able to choose any subfolder of this root)
library(shiny)
library(shinyFiles)
library(xlsx)
ui <- shinyUI(fluidPage(
titlePanel("Example"),
shinySaveButton("save", "Save file", "Save file as ...", filetype=list(xlsx="xlsx"))
))
server <- shinyServer(function(input, output, session) {
observe({
volumes <- c("UserFolder"="D:/Data")
shinyFileSave(input, "save", roots=volumes, session=session)
fileinfo <- parseSavePath(volumes, input$save)
data <- data.frame(a=c(1,2))
if (nrow(fileinfo) > 0) {
write.xlsx(data, as.character(fileinfo$datapath))
}
})
})
shinyApp(ui = ui, server = server)