How to specify file and path to save a file with R-shiny and shinyFiles?

后端 未结 1 1108
暗喜
暗喜 2021-02-04 20:53

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

相关标签:
1条回答
  • 2021-02-04 21:34

    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)
    
    0 讨论(0)
提交回复
热议问题