上传错误文件时出现 R 闪亮警告消息

R Shiny warning message when wrong file is uploaded

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

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!")


})


}

您真正需要的只是 validate 声明。此外,您需要使用 %in% 函数来检查一个值是否在向量中,仅供参考。以下是 warning/error 的更简单的实现。它利用 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)
      })
    }
    ))