如何解决将警报添加到Reactable复选框
我在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)
我不确定如何将“可反应”复选框的选择与警报联系起来。
对于警报本身,我认为bsplus::bs_embed_popover
可能适用于此,但我不确定。
解决方法
您可以使用模式对话框:
# 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。