【问题标题】:How can I hide\show\toggle certain fields in R shiny modal based on other modal fields如何根据其他模态字段隐藏\显示\切换R闪亮模态中的某些字段
【发布时间】:2021-03-17 23:30:39
【问题描述】:

我希望在闪亮的应用程序中弹出 modal,这取决于用户在模式中的操作, 它会显示或隐藏某些字段。

例如,Modal 包含一个按钮,当按下该按钮时,另一个按钮会出现\消失。

遗憾的是,虽然 observeEvent 检测到 hide\show 按钮的变化,shinyjs::toggle(), shinyjs::hide() 和 shinyjs::show() 无法工作

示例脚本:

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)

【问题讨论】:

    标签: r shiny modal-dialog hide show


    【解决方案1】:

    不用shinyjs 也可以使用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)
    
    

    【讨论】:

      【解决方案2】:

      事实证明,shinyjs 的作者 Dean Attali 亲切地指出, 我没有调用 useShinyjs() 函数。

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-06
        相关资源
        最近更新 更多