R Shiny-在本地上载多个csv,但在Shinyapps.io上不起作用

时间:2018-09-16 14:49:48

标签: r shiny shiny-server shinyapps

我制作了一个应用程序,允许用户上传多个csv文件。

然后将这些csv一起“ rbind”,即“ read.csv”,并在df中添加一列,即文件名。

然后处理df以生成可下载的各种图。这在本地完美运行,但在部署时无法正常运行。我已使用以下代码复制了错误:

警告文件(文件,“ rt”):无法打开文件“ * .csv”:没有此类文件或目录

警告:文件中的错误:无法打开连接

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

)
)

服务器:

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

               })
 }

 )


   }

任何帮助将不胜感激。我搜索了成千上万的SO和其他论坛,但我真的被卡住了。

请帮助

1 个答案:

答案 0 :(得分:0)

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

以下内容应该可以正常工作。

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

               )
)

服务器

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

                   })
    }        
  )   
}