1

我在 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可能适用于此,但我不确定。

4

1 回答 1

0

您可以使用模态对话框:

# 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)

在此处输入图像描述

于 2020-09-13T05:30:07.647 回答