How to trigger a event when selectinput was selected with the same choice

Hi everyone,
I want to lanch a modalDialog when select different choice in a selectinput. But the modalDialog won't be displayed if you select choice which is same as old one.
I have known that react won't be triggered if input don't change the value. Is there any method to solve the issue ?

library(shiny)
ui=fluidPage(
  uiOutput("output1")
)

server= function(input,output,session) {
  choices <- reactiveVal(c("none","age","gender","work","so no"))
  
  output$output1<-renderUI({
    selectInput("select1","label",choices = choices())
  })
  
  observeEvent(input$select1,{
    switch(input$select1,
           "age"= {
             showModal(modalDialog(fluidPage({}),title = "Some age settings"))
           },
           "gender"= {
             showModal(modalDialog(fluidPage({}),title = "Some gender settings"))
           }
    )
  })
}
shinyApp(ui=ui,server = server)

Normally, the issue solved in basic javescript is to setting it to default value in onclick event. However, updateSelect input in onclick function of "shinyjs" will close the dropdown of selectinput so it can't be select anymore.

library(shiny)
library(shinyjs)

ui=fluidPage(
  useShinyjs(),
  uiOutput("output1")
)

server= function(input,output,session) {
  
  
  choices <- reactiveVal(c("none","age","gender","work","so no"))
  
  output$output1<-renderUI({
    selectInput("select1","label",choices = choices())
  })
  
  observeEvent(input$select1,{
    switch(input$select1,
           "age"= {
             showModal(modalDialog(fluidPage({}),title = "Some age settings"))
           },
           "gender"= {
             showModal(modalDialog(fluidPage({}),title = "Some gender settings"))
           }
    )
  })
  
  onclick("select1",{
    updateSelectInput(session = session,"select1",selected = "none")
  })
}

shinyApp(ui=ui,server = server)

Can you give me some tips
Thanks,
DY

Here you can find a related GitHub issue:

In this case (as a workaround) you could reset your selectInput after closing the modal:

library(shiny)

ui <- fluidPage(uiOutput("output1"))

server = function(input, output, session) {
  choices <- reactiveVal(c("none", "age", "gender", "work", "so no"))
  
  output$output1 <- renderUI({
    selectInput("select1", "label", choices = choices())
  })
  
  observeEvent(input$select1, {
    switch(input$select1,
           "age" = {
             showModal(modalDialog(
               fluidPage({
               }),
               title = "Some age settings",
               footer = actionButton("dismiss", "Dismiss")
             ))
           },
           "gender" = {
             showModal(modalDialog(
               fluidPage({
               }),
               title = "Some gender settings",
               footer = actionButton("dismiss", "Dismiss")
             ))
           })
  })
  
  observeEvent(input$dismiss, {
    updateSelectInput(session = session, "select1", selected = "none")
    removeModal()
  })
}
shinyApp(ui = ui, server = server)

Here is a different concept; for any existing selection whose modal has closed; enable a Re-Launch button; to see the modal again.

library(shiny)


ui=fluidPage(
  uiOutput("output1"),
  uiOutput("o2")
)

server= function(input,output,session) {
  choices <- reactiveVal(c("none","age","gender","work","so no"))
  
  output$o2 <- renderUI({
    ui_to_use <- actionButton("btn1","Re-Launch","disabled"=NA)
    if(isTruthy(input$select1)){
      if(input$select1 %in% c("age","gender")){
        ui_to_use <- actionButton("btn1","Re-Launch")
      }
    }
    ui_to_use
  })
  
  output$output1<-renderUI({
        selectInput("select1","label",choices = choices())
  })
  
  observeEvent(list(input$select1,
                    input$btn1),{
    switch(req(input$select1),
           "age"= {
             showModal(modalDialog(fluidPage({}),title = "Some age settings"))
           },
           "gender"= {
             showModal(modalDialog(fluidPage({}),title = "Some gender settings"))
           }
    )
  })
}
shinyApp(ui=ui,server = server)

Thanks for your suggestion, but the real environment would has a bound of selectinputs been placed and if they were set to default after closing the each modal, it will be uncomfortable to define which has been set. Maybe I can make a parameter list to display settings later.

Adding buttons is a good ideal, I have thought about it. I will try to find some awsome css style to improve this solution.

For now, I have developed a jquery script to simulate the open process. The selector can work properly, even someone click it incessantly. That's interesting.

library(shiny)
library(shinyjs)

ui=fluidPage(
  useShinyjs(),
  uiOutput("output1")
)

server= function(input,output,session) {
  # reactivevalue defination
  timeStamp <- reactiveValues()
  timeStamp$t <-0
  #
  
  choices <- reactiveVal(c("none","age","gender","work","so no"))
  
  output$output1<-renderUI({
    selectInput("select1","label",choices = choices())
  })
  
  observeEvent(input$select1,{
    switch(input$select1,
           "age"= {
             showModal(modalDialog(fluidPage({}),title = "Some age settings"))
           },
           "gender"= {
             showModal(modalDialog(fluidPage({}),title = "Some gender settings"))
           }
    )
  })
  
  onclick("select1",{
    req((Sys.time() - timeStamp$t) > 1)  # here can change time interval
    timeStamp$t <- Sys.time()
    
    runjs("
          $(\"#select1+ div div.option[data-value=\'none\']\").click();
          $(\"#select1+ div div:first\").click()
    ")
  })
}

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.