【问题标题】:Add Alert to Reactable Checkboxes向可反应的复选框添加警报
【发布时间】:2020-12-31 03:11:12
【问题描述】:

我在 Shiny App 中有一个 Reactable 表。

我希望将允许选中的复选框数量限制为三个。理想情况下,在选择第四个复选框时,鼠标旁边会出现一个小警报框,该鼠标表示“最多3盒”。

这里是限制复选框数量为三个的代码:

# Demonstrate popover when max check boxes reached
# See: https://ijlyttle.shinyapps.io/tooltip_popover_modal/_w_8a89c681/#

library("shiny")
library("reactable")
library("bsplus")

ui <- fluidPage(
  reactableOutput("table"),
  verbatimTextOutput("table_state"),
  
  use_bs_popover() 
)
server <- function(input, output, session) {
  output$table <- renderReactable({
    reactable(
      iris,
      showPageSizeOptions = TRUE,
      selection = "multiple"
    )
  }) 
  
  output$table_state <- renderPrint({
    state <- req(getReactableState("table"))
   
    # Restrict number of checked boxes
    boxes_checked <- state[[4]]
  
    # Which boxes are checked
    print(boxes_checked)
    num_boxes_checked <- (length(boxes_checked))
    
    # Restrict number of checked boxes
    if (num_boxes_checked > 3) {
       updateReactable("table", selected = boxes_checked[1:num_boxes_checked - 1])
    }
  })

}
shinyApp(ui, server)

我不确定如何将 Reactable 复选框的选择与警报联系起来。

对于警报本身,我认为bsplus::bs_embed_popover 可能适用于此,但我不确定。

【问题讨论】:

    标签: r shiny reactable


    【解决方案1】:

    您可以使用模式对话框:

    # Demonstrate popover when max check boxes reached
    # See: https://ijlyttle.shinyapps.io/tooltip_popover_modal/_w_8a89c681/#
    
    library("shiny")
    library("reactable")
    library("bsplus")
    
    ui <- fluidPage(
      reactableOutput("table"),
      verbatimTextOutput("table_state"),
      
      use_bs_popover() 
    )
    server <- function(input, output, session) {
      output$table <- renderReactable({
        reactable(
          iris,
          showPageSizeOptions = TRUE,
          selection = "multiple"
        )
      }) 
      
      output$table_state <- renderPrint({
        state <- req(getReactableState("table"))
        
        # Restrict number of checked boxes
        boxes_checked <- state[[4]]
        
        # Which boxes are checked
        print(boxes_checked)
        num_boxes_checked <- (length(boxes_checked))
        
        # Restrict number of checked boxes
        if (num_boxes_checked > 3) {
          showModal(modalDialog(
            title = "Error!",
            "Max 3 checkboxes!",
            easyClose = TRUE
          ))
          updateReactable("table", selected = boxes_checked[1:num_boxes_checked - 1])
        }
      })
      
    }
    shinyApp(ui, server)
    

    【讨论】:

    猜你喜欢
    • 2012-04-03
    • 2019-12-11
    • 1970-01-01
    • 2010-11-01
    • 2013-04-10
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多