r Shiny-将所有文件从ShinyDirChoose文件夹上载到服务器

杰罗恩·斯派布瑞克(Jeroen Speybroeck)

我使用保存了用户定义文件夹的路径shinyDirChoose现在,我想从该用户的文件夹上载文件,但是我不知道该怎么做。1)全部在服务器端?2)以fileInput某种方式提供文件路径

这就是我为应该上传的三个文件构造文件路径的方式。

### ui end, to browse to desired folder
ui = fluidPage(shinyDirButton('directory', 'Folder select', 'Please select a folder'))

### extracting the folder path
server = function(input, output, session) {
    volumes <- getVolumes()
    shinyDirChoose(input, 'directory', roots=volumes, session=session)
    path1 <- reactive({
       return(print(parseDirPath(volumes, input$directory)))
    })

### constructing the 3 file paths
datpat <- renderText({
    req(nchar(path1())>0)
    datpat <- paste0(path1(),"/data.csv")
  })
vispat <- renderText({
    req(nchar(path1())>0)
    vispat <- paste0(path1(),"/visit.csv")
  })
statpat <- renderText({
   req(nchar(path1())>0)
   statpat <- paste0(path1(),"/statvisit.csv")
})

因此,现在有了这些路径,但是如何使用它们将相关内容上载到服务器?一个简单的read.csv不幸的是不能解决问题。

编辑-但还没有...

在@SBista提供的巨大帮助下进一步工作,我认为我正在实现自己的目标,但是请参见下面的代码...

volumes <- getVolumes()
shinyDirChoose(input, 'directory', roots=volumes, session=session)
path1 <- reactive({
  return(print(parseDirPath(volumes, input$directory)))
})

observe({
  if(!is.null(path1)){
    ### vis1
    vis1 <- reactive({
      datpat <- paste0(path1(),"/visit.csv")
      vis <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                      stringsAsFactors = FALSE)
      vis
    })
    ### dataruw1
    dataruw1 <- reactive({
      datpat <- paste0(path1(),"/data.csv")
      dataruw <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                          stringsAsFactors = FALSE)
      dataruw
    })
  }
})

不幸的是,dataruw1并且vis1似乎没有生成,因为在尝试将实际数据与dataruw1()一起使用时出现“找不到函数”错误vis1()我想念什么?任何的想法?在此先多谢!

杰罗恩·斯派布瑞克(Jeroen Speybroeck)

很高兴找到解决方案!再次感谢SBista,尽管我做的有些不同。如果您浏览到包含data.csv文件的文件夹,则下面的代码不仅是一大块(如我的原始文章一样),而且还具有完整的功能

library(shiny)
library(shinyFiles)
library(htmltools)


##############################################################################

ui = navbarPage(
  HTML("Title"),
  tabPanel(HTML("<font size=3>Start</font>"),
           sidebarPanel(width = 2,
                        shinyDirButton('directory', 'Folder select', 'Please select a folder'),
                        checkboxInput('header', 'Header', TRUE),
                        radioButtons('sep', 'Separator', c(Comma=',',Semicolon=';',Tab='\t'), selected=';'),
                        radioButtons('quote', 'Quote', c(None='','Double Quote'='"','Single Quote'="'"), selected='"')),
           mainPanel(
             fluidRow(
               column(6, tags$b(textOutput("text")))),
             tags$hr(),
             fluidRow(
               column(6, dataTableOutput("table"))
             )
           )
  )
)

server = function(input, output, session) {

  volumes <- getVolumes()
  shinyDirChoose(input, 'directory', roots=volumes, session=session)
  path1 <- reactive({
    return(print(parseDirPath(volumes, input$directory)))
  })

  dataruw1 <- eventReactive(input$directory, {
    datpat <- paste0(path1(),"/data.csv")
    dataruw <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                        stringsAsFactors = FALSE)
    dataruw
  })

  output$text <- renderText({
    path1()
  })

  output$table <- renderDataTable({
    dataruw1()
  })

}

shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章