Create alert for when a file is incorrectly loaded in a shiny app

I created an error alert for when a file of format other than ".xlsx" is loaded in fileInput. For that I created sendSweetAlert. This is working. What I would like now is the following: Whenever I load a ".xlsx" file, apparently the code does not give any error, however I would like to accept only ".xlsx" files, which are actually usable by my function.

The dataset I use can be downloaded from this link: encurtador.com.br/dqsOQ

library(shiny)
library(shinyWidgets)
library(ggplot2)
library(shinythemes)
library(rdist)
library(openxlsx) 
library(geosphere)
library(rgdal)


function.cl<-function(df,k){
  
  
  #clusters
  coordinates<-df[c("Latitude","Longitude")]
  d<-as.dist(distm(coordinates[,2:1]))
  fit.average<-hclust(d,method="average") 
  clusters<-cutree(fit.average, k) 
  nclusters<-matrix(table(clusters))  
  df$cluster <- clusters 
  
  #all cluster data df1 and specific cluster df_spec_clust
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-as.factor(clusters)
  
  #Colors
  my_colors <- rainbow(length(df1$cluster))
  names(my_colors) <- df1$cluster
  
  #Scatter Plot for all clusters
  g <- ggplot(data = df1,  aes(x=Longitude, y=Latitude, color=cluster)) + 
    geom_point(aes(x=Longitude, y=Latitude), size = 4) +
    scale_color_manual("Legend", values = my_colors)
  plotGD <- g
  
  
  return(list(
    "Plot" = plotGD
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Excel import"),
                                accept = c(".xlsx"),
                                multiple= T),  
                      sidebarLayout(
                        sidebarPanel(
                          
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 4, value = 3)),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                        
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL,clear=FALSE)
  
  observeEvent(input$data, {
    if(any(grepl(".xlsx",input$data$name))){
      v$df <- read_excel(input$data$datapath)
      showModal(modalDialog("Once you have uploaded the file, it is necessary to choose the filter options below.", easyClose=TRUE, footer = modalButton("OK")))
    }
      else{
      sendSweetAlert(
        session = session,
        title = "Erro!!",
        text = "File Wrong.",
        type = "error"
      )
      return(NULL)
    }
    v$clear <- TRUE
  })
  
  Modelcl<-reactive({
    req(v$df)
    function.cl(v$df, input$Slider)
  })

  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.