【问题标题】:How to add a folder on Shinyio to upload multiple files如何在 Shinyio 上添加文件夹以上传多个文件
【发布时间】:2018-08-28 20:45:53
【问题描述】:

我有近 30 个不同名称的文件要上传。我正在寻找一个本地数据路径,然后可以用来上传许多文件。

我添加了一个非常简化的代码版本,所以我不会混淆人们。这是使用library(shinyFiles),特别是在用户界面中使用shinyDirButton,在服务器中使用shinyDirChoose

这是在 R Studio 上本地运行的,但是当我将它添加到我的 shinyio 应用程序时,我无法让本地文件夹显示在我的应用程序上。

有解决办法吗?我试过fileInput,但它似乎也不起作用。

ui<-fluidPage(
  mainPanel("Hydro-BID-Opt",
            tabsetPanel(
              tabPanel("Information required for the model",
                       numericInput("Res", label = h3("Total Res"), 
                                    min = 1, max = 25, 
                                    value = 3),
                       numericInput("Muns", label = h3("Total Users"), 
                                    min = 1, max = 150, 
                                    value = 5),
                        numericInput("Time", label = h3("Total Number of Months"), 
                                    min = 0, max = 60, 
                                    value = 12)
              ),
              tabPanel("Adding the folder",
                       shinyDirButton("directory", "Please add your data path where the csv files are stored", "Please select a folder", FALSE)
              ),
              tabPanel("Results", 
                       h3("Results for Cost"),
                       textOutput("Table_Cost")
              )
              )))


server<-function(input, output, session) {
  Mo <- reactive({input$Time})
  R <- reactive({input$Res})
  Mu <- reactive({input$Muns})

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

  ## ResMax
  Resmaxcsv <- eventReactive(input$directory, {
    datpath_two <- paste0(path1(),"/MRC.csv")
    dataruw_Resmaxcsv <- read.csv(datpath_two, check.names=F, header = T)
    dataruw_Resmaxcsv
  })


  ## Cost
  Costcsv <- eventReactive(input$directory, {
    datpath_seven <- paste0(path1(),"/Cost.csv")
    dataruw_Costcsv <- read.csv(datpath_seven, check.names=F, header = T)
    dataruw_Costcsv
  })

  #### Running the model
  Test <- reactive({
    nT<-Mo()
    nR<-R()
    nM<-Mu()

    ##  ResMax
    resmaxcapacity<-Resmaxcsv()
    SCmax_rt<-array(data = resmaxcapacity[,2] * 1e-6, dim = c(nR, nT))

    ## Cost
    costcsv<-Costcsv()
    cost<-as.matrix(costcsv[,2:(nM+1)])
    costQ <- array(data = cost[1:nR, 1:nM], dim = c( nR, nM, nT))

    App<-apply(costQ, MARGIN = c(1,3), mean)
    TOTAL<-SCmax_rt+App
    print(TOTAL)
  })


  output$Table_Cost<-renderPrint({Test()})
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: shiny shiny-server


    【解决方案1】:

    我知道这有点晚了,但我想我已经找到了将文件夹上传到闪亮应用程序的解决方案。我已经在 chrome 浏览器上对其进行了测试,它也适用于 Edge 和 Firefox。

    您可以在此处查看演示:

    https://absuag.shinyapps.io/FileExplorer/

    上传一个文件夹,它将打印在主面板中选择的所有文件。

    我已使用 HTMLInputElement.webkitdirectory 上传文件夹。这是闪亮应用的源代码。

    
    library(shiny)
    library(shinydashboard)
    ui <- dashboardPage(skin = "yellow",
                        # Title
                        dashboardHeader(title = "File-Explorer",titleWidth=140,uiOutput("logoutbtn")),
                        dashboardSidebar(
                          tags$button(HTML('<input id = "folderPath1" type="file" webkitdirectory mozdirectory /> 
                                                        <input id = "folderPath2" type="file" webkitdirectory mozdirectory />'))),
                        dashboardBody(
                                      textOutput("text1"),
                                      textOutput("text2"),
                                      tableOutput("table1")
                          )
    )
    server <- function(input,output){
        observeEvent(input$folderPath1,{
            output$text1 <- renderText({
                    print(paste0(input$folderPath1$name,collapse=","))
            })
            output$table1 <- renderTable({
                df <- read.csv(input$folderPath1$datapath[1])
            })
    })
      observeEvent(input$folderPath2,{
        output$text2 <- renderText({
          print(paste0(input$folderPath2$name,collapse=","))
        })
      })
    }
    
    shinyApp(ui,server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多