如何使用R-shiny和shinyFiles指定文件和path来保存文件?

我正在与R(shiny),并希望将数据框保存为Excel文件。 为此,我使用“shinyFiles”包,以便用户可以指定excel文件的存储位置:

server.R库(shiny)库(shinyFiles)

shinyServer(function(input, output, session) { ## ShinyFiles : get the user favorite directory volumes=c(home = '~/'), shinyDirChoose(input, 'dir', roots=volumes, filetypes = c('','xlsx')), output$dir.res <- renderPrint({parseDirPath(volumes, input$dir)}), ## Button to save the file observeEvent(input$button.save, { ## A standard file name A <- "name" B <- "family if( input$text == "File name..." ) outFile <- paste( A, "_", B, ".xlsx", sep="" ) ## Append the path to the file name outFile <- paste( parseDirPath(volumes, input$path.out), outFile, sep="/" ) ## The data to be saved x=seq(from=0,to=10,by=1) d = data.frame( x ) write.xlsx( d, outFile ) } 

和ui.R

 library(shiny) library(shinyFiles) shinyUI(fluidPage(sidebarLayout( ## Choose the output directory shinyDirButton("dir", "Choose directory", "Upload"), ## Choose the output file name textInput("text", label = "", value = "File name..."), ## Save the data actionButton("button.save", "Save the file"), ## Give the path selected verbatimTextOutput("dir.res") ))) 

尽pipe所有类似的问题find的例子,我已经尝试了2小时(羞耻..),并会感谢帮助

这是一个工作的例子。 同样,这也假定您在自己的计算机上运行应用程序,并且允许用户访问此计算机上的文件夹。 您可以设置允许用户保存文件的根文件夹(请参阅UserFolder ,用户将能够select此根目录的任何子文件夹)

 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)