【问题标题】:R Shiny warning message when wrong file is uploaded上传错误文件时的R闪亮警告消息
【发布时间】:2015-05-15 09:02:15
【问题描述】:

我想在我的闪亮应用程序中有一个功能,当有人上传不是 .csv 格式的文件时,他们会收到警告,当他们以 .csv 格式上传时,它会打印表格。这是我的用户界面代码

shinyUI(
tabPanel("File Upload",
                      h4("File Upload"),
                      sidebarLayout(
                        sidebarPanel(
                          fileInput('file1', 'Choose CSV file to upload',
                                    accept = c(
                                      'text/csv',
                                      'text/comma-separated-values',
                                      'text/tab-separated-values',
                                      'text/plain',
                                      '.csv',
                                      '.tsv'
                                    )
                          )
                        ),
                        mainPanel(
                          tableOutput('upload'),
                          h1(textOutput("warning1"))
                          )


                      )

               )
)

和我的服务器代码

shinyServer(function(input, output) {

output$upload <- renderTable({


  #assign uploaded file to a variable
  File <- input$file1

  #catches null exception
  if (is.null(File))
    return(NULL)

  read.csv(File$datapath)

})

output$warning1 <- renderPrint({

  upload<- input$file1
  if (is.null(upload))
    return(NULL)

  if (upload$type != c(
                          'text/csv',
                          'text/comma-separated-values',
                          'text/tab-separated-values',
                          'text/plain',
                          '.csv',
                          '.tsv'
                        )    
      )
  return ("Wrong File Format try again!")


})


}

【问题讨论】:

  • 那么问题是什么,你得到的错误是什么?
  • 我的问题是它似乎永远无法捕捉到正确的文件类型。即使不是 csv 格式,它也会打印每种格式。 $type 似乎不起作用并发送一个空值

标签: r csv file-upload shiny rstudio


【解决方案1】:

您真正需要的是validate 声明。此外,您需要使用 %in% 函数来检查值是否在向量中,仅供参考。以下是您的警告/错误的更简单的实现。它利用了tools 包中的file_ext 函数。

library(shiny)
library(tools)

runApp(
  list(
    ui = tabPanel("File Upload",
               h4("File Upload"),
               sidebarLayout(
                 sidebarPanel(
                   fileInput('file1', 'Choose CSV file to upload',
                             accept = c(
                               'text/csv',
                               'text/comma-separated-values',
                               'text/tab-separated-values',
                               'text/plain',
                               'csv',
                               'tsv'
                             )
                   )
                 ),
                 mainPanel(
                   tableOutput('upload')
                 )
               )

      ),
    server = function(input, output){
      output$upload <- renderTable({

        #assign uploaded file to a variable
        File <- input$file1        

        #catches null exception
        if (is.null(File))
          return(NULL)

        validate(
          need(file_ext(File$name) %in% c(
            'text/csv',
            'text/comma-separated-values',
            'text/tab-separated-values',
            'text/plain',
            'csv',
            'tsv'
          ), "Wrong File Format try again!"))

        read.csv(File$datapath)
      })
    }
    ))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 2014-08-30
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多