Adjust code to show showModal

Can you help me understand and if possible correct my code, because when I press filters 1, 2 and 3, showModal does not appear and I believe it should.

library(shiny)

function.cl<-function(filter1,filter2,filter3){
  
  df <- structure(
    list(date = c("2021-01-01","2021-01-02","2021-01-03","2021-01-04","2021-01-05"),
         d1 = c(0,1,4,5,6), d2 = c(2,4,5,6,7)),class = "data.frame", row.names = c(NA, -5L))
}   

ui <- fluidPage(
  
  ui <- shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                          br(),
                          
                          tabPanel("",
                                   sidebarLayout(
                                     sidebarPanel(
                                       
                                       ui <- fluidPage(
                                         
                                         radioButtons("filter1", "Distribution type:",
                                                      c("Normal" = "norm",
                                                        "Uniform" = "unif",
                                                        "Log-normal" = "lnorm",
                                                        "Exponential" = "exp"),selected=""),
                                         
                                         radioButtons("filter2", "Distribution type:",
                                                      c("Normal" = "norm",
                                                        "Uniform" = "unif",
                                                        "Log-normal" = "lnorm",
                                                        "Exponential" = "exp"),selected=""),
                                         
                                         radioButtons("filter3", "Distribution type:",
                                                      c("Normal" = "norm",
                                                        "Uniform" = "unif",
                                                        "Log-normal" = "lnorm",
                                                        "Exponential" = "exp"),selected=""),
                                         
                                         radioButtons("filter4", "Distribution type:",
                                                      c("Normal" = "norm",
                                                        "Uniform" = "unif",
                                                        "Log-normal" = "lnorm",
                                                        "Exponential" = "exp"),selected=""),
                                         
                                         radioButtons("filter5", "Distribution type:",
                                                      c("Normal" = "norm",
                                                        "Uniform" = "unif",
                                                        "Log-normal" = "lnorm",
                                                        "Exponential" = "exp"),selected=""))

                                     ),
                                     
                                     mainPanel(
                                     ))
                          )))


server <- function(input, output,session) {
  
  v <- reactiveValues(df = NULL, clear=FALSE)

  ModelCl<-reactive({if (!is.null(v$df)) {
    req(input$filter1,input$filter2,input$filter3)
    showModal(modalDialog("Wait a moment...", footer=NULL))
    on.exit(removeModal())
    function.cl(input$filter1,input$filter2,input$filter3,input$filter4,input$filter5)
  }
  })

}

shinyApp(ui = ui, server = server)
 
  ModelCl<-reactive({
    req(input$filter1,input$filter2,input$filter3)
    showModal(modalDialog("Wait a moment...", footer=NULL))
    on.exit(removeModal())
    function.cl(input$filter1,input$filter2,input$filter3)
  })

  observe(ModelCl())
  1. v$df was never going to be not null so ModelCl was never going to have content
  2. ModelCl is a reactive() and so would only run if it was referenced by another item in the reactive graph that ends in an observe or render type action, as shiny is lazy, if ModelCl is not used downstream for anything its not evaluated. Therefore I observe it, so that shiny knows that I want it to run.
  3. the function.cl was being called with more parameters than it was defined to accept so I had to reduce that.
  4. finally the ui, was set to use shinytheme which is from a package shinythemes, I had to add that.
1 Like

This topic was automatically closed 7 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.