Antonio
Antonio

Reputation: 1111

Error alert when the database is wrong in Shiny

I would like to know how I can create a general alert, if the database that is loaded in fileInput does not correspond to the ideal base for my code, as there may be several problems, such as: Error in [.data.frame: undefined columns selected, error in hclust: NA / NaN / Inf, among other errors. So, is there any way to do this? Show an alert if I have a problem with the loaded database? I inserted below an executable code just for testing. A database can be downloaded from the following website:

https://github.com/JovaniSouza/JovaniSouza5/blob/master/Example.xlsx

library(shiny)
library(ggplot2)
library(shinythemes)
library(rdist)
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")), 
                      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)
  observeEvent(input$data, {
    v$df <- read_excel(input$data$datapath)
  })
  
  
  Modelcl<-reactive({if (!is.null(v$df)) {
    function.cl(v$df,input$Slider)
  }
  })
  
  
  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  
  
}

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 126

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

Maybe something like this (I have not tried). This requires the shinyWidgets package for sendSweetAlert.

Modelcl <- reactive({
  req(v$df)
  out <- NULL
  tryCatch({
    out <<- function.cl(v$df, input$Slider)
  }, error = function(e){
    sendSweetAlert(
      session, 
      "An error occured",
      "Try to upload another file.", 
      "error"
    )
  })
  out
})

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

Upvotes: 1

Related Questions