【问题标题】:r shiny - upload all files from shinyDirChoose folder to serverr shiny - 将所有文件从 shinyDirChoose 文件夹上传到服务器
【发布时间】:2017-11-10 06:48:50
【问题描述】:

我使用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
    })
  }
})

不幸的是,dataruw1vis1 似乎没有生成,因为在尝试将实际数据与 dataruw1()vis1() 一起使用时出现“找不到函数”错误。我错过了什么?任何想法?提前非常感谢!

【问题讨论】:

  • 尝试在服务器函数的开头声明reactiveValue,并在读取时分配值。您编辑的代码的问题是您的reactive expression 的范围被限制在if 条件内。
  • “将所有文件从 shinyDirChoose 文件夹上传到服务器”是否已解决?
  • @RanonKahn 我只能说下面的答案(或者更确切地说是第二个答案的 cmets 中的内容)符合我的需求......
  • 我的印象是您试图通过身份验证等将文件夹的所有或选定内容移动到 FTP 服务器等。
  • 不,只是上传到shinyapps.io的服务器,对不起。

标签: r shiny


【解决方案1】:

我已修改您的应用程序以证明您可以使用read.csv 上传文件。为了演示,我只从文件夹中读取一个文件,并在数据表中显示读取的数据框。

  library(shiny)
  library(shinyFiles)

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

  ### 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
    observe({
      if(!is.null(path1)){
        output$datpat <- renderTable({
          req(nchar(path1())>0)
          datpat <- paste0(path1(),"/data.csv")
          dat <- read.csv(datpat)
          dat
        })

      }
    })

  }

  shinyApp(ui = ui, server = server)

希望对你有帮助!

【讨论】:

  • 非常感谢!这是我关于 SO 的第一个问题,所以很高兴得到如此迅速的答案。我已经编辑了我的 OP 以显示我从你的代码中得到了什么,但它还不能工作(见帖子中的编辑)......
  • 解决方案添加为答案。不幸的是,不能在服务器上工作,只能在本地工作。
  • shinyFiles 仅在本地工作。您可以在服务器上查看this 链接以进行目录选择。
  • 再次感谢!我在fileInput 中使用了多个文件选项,它也可以正常工作。 stackoverflow.com/questions/44464823/…
【解决方案2】:

很高兴找到解决方案!再次感谢 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))

【讨论】:

    猜你喜欢
    • 2019-06-08
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2010-12-28
    • 2011-10-28
    • 2016-05-26
    相关资源
    最近更新 更多