简体   繁体   English

如何根据其他模态字段隐藏\显示\切换 R shiny 模态中的某些字段

[英]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.我希望在 shiny 应用程序中有一个弹出模式,根据用户在模式中的操作,它会显示或隐藏某些字段。

For example, the Modal includes a button that when pressed, another button would apear\disappear.例如,Modal 包含一个按钮,当按下该按钮时,另一个按钮会出现\消失。

sadly, although the observeEvent detects a change in the hide\show button, shinyjs::toggle(), shinyjs::hide() and shinyjs::show() fail to work遗憾的是,虽然 observeEvent 检测到 hide\show 按钮发生了变化,但 shinyjs::toggle()、shinyjs::hide() 和 shinyjs::show() 无法工作

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() :你可以通过使用conditionalPanel()来做到这一点,而无需shinyjs

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.事实证明,shinyjs 的作者 Dean Attali 友善地指出,我没有调用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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