R Shiny warning message when wrong file is uploade

2019-09-08 09:27发布

问题:

I want to have a feature in my shiny app that when someone uploads a file which is not in .csv format they recieve a warning and when they do upload in .csv format it prints the table. here is my UI code

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


                      )

               )
)

and my Server code

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


})


}

回答1:

All you really need is a validate statement. Also, you would need to use the %in% function to check if a value is in a vector, just FYI. The following is a much simpler implementation for your warning/error. It utilizes the file_ext function from the tools package.

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