简体   繁体   中英

How can I hide\show\toggle certain fields in R shiny modal based on other modal fields

I wish to have a popout modal within a shiny app that depending on the user's action within the modal, it would show or hide certain fields.

For example, the Modal includes a button that when pressed, another button would apear\disappear.

sadly, although the observeEvent detects a change in the hide\show button, shinyjs::toggle(), shinyjs::hide() and shinyjs::show() fail to work

example script:

library(shiny)


ui <- fluidPage(
  actionButton("show_modal", "show modal"),
)

server <- function(input, output) {

  observeEvent(input$show_modal, {
    
          
    showModal(
      
      modalDialog(footer = NULL,easyClose = T,
                  
                  tagList(
                    
                    fluidRow(
                    
                    box(status = "primary", width = 6, style = "direction: ltr",
                        
                        actionButton("toggle_btn", "show or hide second button")
                        
                        
                        )),
                    
                    fluidRow(
                      
                    box(status = "success", width = 6, style = "direction: ltr",
                        
                        
                        actionButton("box_btn", "Box!")
                        
                        
                    ))
                    
                    
                  )
      ))
  })
  

  observeEvent(input$toggle_btn, {
    
    shinyjs::toggle("box_btn")
    cat("\npresentation button pressed\n")
    
  })
  

}

shinyApp(ui, server)

You can do it without shinyjs by using conditionalPanel() :

library(shiny)

ui <- fluidPage(
  actionButton("show_modal", "show modal"),
)

server <- function(input, output) {
  
  rv <- reactiveValues(show_btn = FALSE)
  
  observeEvent(input$toggle_btn, {
    rv$show_btn <- !rv$show_btn
  })
  output$show_btn <- reactive({rv$show_btn})
  outputOptions(output, "show_btn", suspendWhenHidden = FALSE)
  
  
  observeEvent(input$show_modal, {
    
    # add_path_to_existing_offers_DB(user = user)
    
    showModal(
      
      modalDialog(
        footer = NULL,
        easyClose = T,
        tagList(
          
          fluidRow(

                actionButton("toggle_btn", "show or hide second button")

          ),
          
          conditionalPanel(
            condition = "output.show_btn == true",
            
            fluidRow(
              
              actionButton("box_btn", "Box!")
              
            )
          )
        )
      )
    )
  })
  

}

shinyApp(ui, server)

Turns out as Dean Attali the author of shinyjs pointed out kindly, that I failed to call useShinyjs() function.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  **useShinyjs(),**
  actionButton("show_modal", "show modal"),
)

server <- function(input, output) {

  observeEvent(input$show_modal, {
    
    
    showModal(
      
      modalDialog(footer = NULL,easyClose = T,
                  
                  tagList(
                    
                    fluidRow(
                    
                    box(status = "primary", width = 6, style = "direction: ltr",
                        
                        actionButton("toggle_btn", "show or hide second button")
                        
                        
                        )),
                    
                    fluidRow(
                      
                    box(status = "success", width = 6, style = "direction: ltr",
                        
                        
                        actionButton("box_btn", "Box!")
                        
                        
                    ))
                    
                    
                  )
      ))
  })
  

  observeEvent(input$toggle_btn, {
    
    shinyjs::toggle("box_btn")
    cat("\npresentation button pressed\n")
    
  })
  

}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM