How to add a folder on Shinyio to upload multiple files


#1

Hi,

I have also asked this question on Stakoverflow (https://stackoverflow.com/questions/49377401/how-to-add-a-folder-on-shinyio-to-upload-multiple-files) with no answers, so I thought I would ask here

I have close to 30 files with different names to upload. I was looking to choose a local data path which then can be used to upload the many files.

I have added a very simplified version of my code so I do not confuse people. This is using library(shinyFiles), specifically shinyDirButton in the ui and shinyDirChoose in the server.

This is running locally on R Studio, but when I add it to my shinyio app, I am unable to get the local folders to show up on my app.

Is there a solution? I tried fileInput, but it does not seem to be working either.

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)


#2

I'm not sure I completely understand - are you trying to upload folders / files during the publishing process or as a user of your application?

For the former, if you are simply trying to streamline the publishing process, you can look into the rsconnect package as a way to script your desired publishing process.

For the latter, this article might be helpful, along with this example.

Usually, files that are uploaded at publish time will be in subdirectories of the app folder. Similarly, usually files written after the app is published are usually either temporarily written to /tmp or local to the app. Persistent data needs to be thought about deeply in a Shiny app on shinyapps.io, though, since redeployments will destroy this data if it is not persisted elsewhere. This article speaks to the topic directly.


#3

Dear Cole,

Thank you for this information. I had about 30-40 files with different names, and I wanted to upload all of them at the same time, and then read specific documents within it, using the specific names of the csvs. I was hoping that I could select a folder path and do it. But it did not seem possible.

I was able to get it to work using fileInput with multiple=TRUE where I selected all the csv files from the folder. I was looking for ways in which I could use the name of the csv file in read.csv. Here is a working solution I used for it (using the same code as above) for anyone who would want to do the same thing ----

library(shiny)

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",
                       fileInput("directory", label=h5("Upload all the files as described in the manual"), accept = ".csv", multiple = TRUE)
              ),
              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})
  
  #######ResMax
  Resmaxcsv <- reactive({
    read.csv(file=input$directory$datapath[input$directory$name=="Maximum_Reservoir_Capacity.csv"] , check.names=F, header = T)
  })
  
  #####Cost
  Costcsv <- reactive({
    read.csv(file=input$directory$datapath[input$directory$name=="Cost.csv"] , check.names=F, header = T)
  })
  
  #####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)