Search code examples
rshinyshiny-servershinyapps

R Shiny - uploading multiple csv works locally but not on shinyapps.io


I have produced an app that allows the user to upload multiple csv files.

These csvs are then 'rbind' together, 'read.csv' and have a column added to the df which is the filename.

The df is then processed to produce various plots which are downloadable. This works perfectly locally but not when deployed. I've replicated the error with the code below:

Warning in file(file, "rt") :cannot open file '*.csv': No such file or directory

Warning: Error in file: cannot open the connection

UI:

    dashboardPage( skin = "black",
   dashboardHeader(title = "myApp"),
   dashboardSidebar(collapsed = TRUE,
   sidebarMenu(
    menuItem("Home", tabName = "dashboard1", icon = icon("home", lib = 
  "glyphicon"))
    ) 
    ),
   dashboardBody(
     tags$head(tags$style(HTML('
      .main-header .logo {
                          font-family: "Times New Roman", serif;
                          font-weight: bold;
                          font-size: 24px;
                          }
                          '))),

tabItems(
  tabItem(tabName = "dashboard1",
          fileInput("file1",
                    label="Input files:",
                    multiple = TRUE),
          downloadButton('plot.pdf', 'Download Data')
  )
  )

)
)

Server:

     library(shiny)
     library(shinydashboard)

     #server start
     function(input, output) {

       testinput<- reactive({
if(is.null(input$file1))
  return ()
else 
{
  nfiles = nrow(input$file1) 
  csv = list()
  for (i in 1 : nfiles)
  {

    csv[[i]] = read.csv(input$file1$datapath[i])

  }

  csv_names <- input$file1[['name']]
  mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.')[[1]][1])))
  View(mydata)
    }
   })

     output$plot.pdf <- downloadHandler(
    filename = function() {
     "plot.pdf"
    },
    content = function(file) {
      withProgress(message = 'Your file is downloading',
               detail = 'This may take a minute or two...', value = 0, {
                 for (i in 1:10) {
                   incProgress(1/10)
                   Sys.sleep(0.5)}

                 pdf(file)
                 print(testinput())
                 dev.off()

               })
 }

 )


   }

Any help would be really appreciated. I have searched tons of SO and other forums and I'm really stuck.

Please help


Solution

  • You should not use csv_names <- input$file1[['name']] in your server, this only return the file name, not file path, so when you use read.csv to read the csv file, the error occured.

    The following should work fine.

    ui

    library(shiny)
    library(shinydashboard)
    
    
    dashboardPage( skin = "black",
                   dashboardHeader(title = "myApp"),
                   dashboardSidebar(collapsed = TRUE,
                                    sidebarMenu(
                                      menuItem("Home", tabName = "dashboard1", icon = icon("home", lib = 
                                                                                             "glyphicon"))
                                    ) 
                   ),
                   dashboardBody(
                     tags$head(tags$style(HTML('
          .main-header .logo {
                              font-family: "Times New Roman", serif;
                              font-weight: bold;
                              font-size: 24px;
                              }
                              '))),
    
                     tabItems(
                       tabItem(tabName = "dashboard1",
                               fileInput("file1",
                                         label="Input files:",
                                         multiple = TRUE),
                               downloadButton('plot.pdf', 'Download Data')
                       )
                     )
    
                   )
    )
    

    server

    library(shiny)
    library(shinydashboard)
    
    #server start
    function(input, output) {
    
      testinput<- reactive({
        if(is.null(input$file1))
          return ()
        else 
        {
          nfiles = nrow(input$file1) 
          csv = list()
          for (i in 1 : nfiles)
          {
    
            csv[[i]] = read.csv(input$file1$datapath[i])
    
          }
    
          csv_names <- input$file1$datapath
          mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(basename(x),'\\.')[[1]][1])))
          mydata
        }
      })
    
      output$plot.pdf <- downloadHandler(
        filename = function() {
          "plot.pdf"
        },
        content = function(file) {
          withProgress(message = 'Your file is downloading',
                       detail = 'This may take a minute or two...', value = 0, {
                         for (i in 1:10) {
                           incProgress(1/10)
                           Sys.sleep(0.5)}
    
                         pdf(file)
                         plot(testinput()[,1])
                         dev.off()
    
                       })
        }        
      )   
    }