R Shiny如何在renderui中的复选框中添加“全选”

时间:2017-07-05 08:38:35

标签: r checkbox shiny

我在R Shiny中使用renderui来生成一个复选框,我想知道如何添加一个'select all'选项,可以用来检查和取消选中所有盒子。 here is an example of the checkbox I want

link显示如何添加“全选”按钮,但不适用于renderui中的复选框。目前我使用的方法是

  output$trmt_id <- renderUI({

if(is.null(ep_info())) {
  return() 
}

trmt_ids <- unique(ep_info()$TRMT_ID)
# add 'selected = ' to pre-select ctrl
checkboxGroupInput("input_trmt_id", "Choose Treatment ID (Include Control)", choices = c(trmt_ids, 'All'))})


observeEvent(input$input_trmt_id, {
if (input$input_trmt_id %in% 'All') {

  updateCheckboxGroupInput(session, "input_trmt_id", "Choose Treatment ID (Include Control)", choices = c(unique(ep_info()$TRMT_ID), 'all'), selected = unique(ep_info()$TRMT_ID))
}})

但是这段代码的问题是用户无法通过再次单击“全部”来取消选中所有框。

你有更好的想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

没有复选框,但pickerInput是一个简单的选择:

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  column(
    width = 4,
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = c("Banana", "Blueberry", "Cherry", "Coconut", "Grapefruit",
                  "Kiwi", "Lemon", "Lime", "Mango", "Orange", "Papaya"),
      options = list(`actions-box` = TRUE, `selected-text-format` = "count > 2",
                     `count-selected-text` = "{0}/{1} fruits"),
      multiple = TRUE
    ),
    verbatimTextOutput(outputId = "res")
  )
)


server <- function(input, output) {
  output$res <- renderPrint({
    input$id
  })
}

shinyApp(ui = ui, server = server)

结果如下:

enter image description here