Home > Back-end >  How to adjust code to show showModal in a shiny code
How to adjust code to show showModal in a shiny code

Time:08-05

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)

CodePudding user response:

You have a few issues. First, as v$df is initially NULL and never set to anything else, !is.null(v$df) is never true and hence that condition is never met. Second, your function takes 3 arguments but are not used in defining df at the top. Third, you are using 5 inputs to call a function that has been defined with only 3. Try this

  observeEvent(req(input$filter1,input$filter2,input$filter3), {
    showModal(modalDialog("Wait a moment...", footer=NULL))
    on.exit(removeModal())
  })
  • Related