1

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

4

1 回答 1

0

你不应该csv_names <- input$file1[['name']]在你的 .. 中使用server,这只会返回文件名,而不是文件路径,所以当你使用read.csv读取 csv 文件时,会发生错误。

以下应该可以正常工作。

用户界面

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')
                   )
                 )

               )
)

服务器

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()

                   })
    }        
  )   
}
于 2018-09-18T01:41:28.323 回答