Antonio
Antonio

Reputation: 1111

Alert message when starting shiny

The executable code below shows an alert when the Exclude farms option is selected. This same alert shows which industries will be excluded. However, I would like to generate a new feature that is if there is no industry to exclude, that is, if the "ind_exclude" of the code is empty. I would like a message alert to appear as soon as shiny starts, saying: "No there are industries to be excluded"

library(shiny)
library(rdist)
library(geosphere)
library(tidyverse)
library(shinyWidgets)
library(shinythemes)

function.cl<-function(df){

  #database df
  df<-structure(list(Industries = c(1,2,3,4,5,6,7), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,-23.8,-23.8), 
                     Longitude = c(-49.8, -49.8, -49.5, -49.8, -49.8,-49.5,-49.8), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))


  coordinates<-subset(df,select=c("Latitude","Longitude")) 
  d<-distm(coordinates[,2:1]) 
  diag(d)<-1000000 
  min_distancia<-as.matrix(apply(d,MARGIN=2,FUN=min))
  limite<-mean(min_distancia)+sd(min_distancia) 

  search_vec <- function(mat, vec, dim = 1, tol = 1e-7, fun = all)
    which(apply(mat, dim, function(x) fun((x - vec) > tol)))
  ind_exclude<-search_vec(min_distancia,limite,fun=any)
  if(is_empty(ind_exclude)==FALSE){
    for (i in 1:dim(as.array(ind_exclude))){
      df<-subset(df,Industries!=ind_exclude[i])}}


  return(list(
    "IND" =  ind_exclude
  ))

}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(

                          selectInput("filter1", h3("Select farms"),
                                      choices = list("All farms" = 1, 
                                                     "Exclude farms" = 2),
                                      selected = 1),


                        ),
                        mainPanel(
                          tabsetPanel())))))  

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df)
  })

  output$ind <- renderTable({
    IND <- ((Modelcl()[[1]]))
  })
  observe({
    if(input$filter1 == 2){
      sendSweetAlert(
        session = session,
        title = "Information!",
        btn_labels = c("Yes", "No"),
        text = tags$div(h5("The industries that need to exclude are:"), 
                        paste(Modelcl()[[1]], collapse = ", ")
        ),

        type = "info"
      )
    }
  })


}

shinyApp(ui = ui, server = server)

Thank you very much!

Upvotes: 0

Views: 370

Answers (1)

bs93
bs93

Reputation: 1316

As @Bruno mentioned, add an observer to the object you want to look for that might be empty. You just need to test if the object is NULL/empty/has no values. Here is an app that uses rlang::is_empty to check if a data.frame is empty. If it is an alert is sent including at the start of the app (I created a new reactive object Modelcl2 that is always empty just to demonstrate the approach of observing with an if statement):

library(shiny)
library(rdist)
library(geosphere)
library(tidyverse)
library(shinyWidgets)
library(shinythemes)

function.cl<-function(df){

  #database df
  df<-structure(list(Industries = c(1,2,3,4,5,6,7), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,-23.8,-23.8), 
                     Longitude = c(-49.8, -49.8, -49.5, -49.8, -49.8,-49.5,-49.8), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))


  coordinates<-subset(df,select=c("Latitude","Longitude")) 
  d<-distm(coordinates[,2:1]) 
  diag(d)<-1000000 
  min_distancia<-as.matrix(apply(d,MARGIN=2,FUN=min))
  limite<-mean(min_distancia)+sd(min_distancia) 

  search_vec <- function(mat, vec, dim = 1, tol = 1e-7, fun = all)
    which(apply(mat, dim, function(x) fun((x - vec) > tol)))
  ind_exclude<-search_vec(min_distancia,limite,fun=any)
  if(is_empty(ind_exclude)==FALSE){
    for (i in 1:dim(as.array(ind_exclude))){
      df<-subset(df,Industries!=ind_exclude[i])}}


  return(list(
    "IND" =  ind_exclude
  ))

}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(

                          selectInput("filter1", h3("Select farms"),
                                      choices = list("All farms" = 1, 
                                                     "Exclude farms" = 2),
                                      selected = 1),


                        ),
                        mainPanel(
                          tabsetPanel())))))  

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df)
  })

  Modelcl2<-reactive({
    data.frame()
  })

  output$ind <- renderTable({
    IND <- ((Modelcl()[[1]]))
  })
  observe({
    if(input$filter1 == 2){
      sendSweetAlert(
        session = session,
        title = "Information!",
        btn_labels = c("Yes", "No"),
        text = tags$div(h5("The industries that need to exclude are:"), 
                        paste(Modelcl()[[1]], collapse = ", ")
        ),

        type = "info"
      )
    }
  })

  observe({
    if(is_empty(Modelcl2())){
      sendSweetAlert(session = session,
                     title = "Hey",
                     btn_labels = c("Yes", "No"),
                     text = "nothing to exclude",

                     type = "info"
      )
    }
  })

}

shinyApp(ui = ui, server = server)

enter image description here

Here is a very minimal example to show to bare bones workflow. You could also do a test if it is a dataframe based on the number of rows if(nrow(dataframe) < 1) or something like that, just find a test that works for the type of object you are working with:

library(shiny)
library(tidyverse)
library(shinyWidgets)


ui <- bootstrapPage()

server <- function(input, output, session) {


  Modelcl2 <-reactive({
    data.frame()
  })

  observe({
    if(rlang::is_empty(Modelcl2())){
      sendSweetAlert(session = session,
                     title = "Hey",
                     btn_labels = c("Yes", "No"),
                     text = "nothing to exclude",

                     type = "info"
      )
    }
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions