【问题标题】:Select/Deselect All Button for shiny variable selection选择/取消选择所有按钮用于闪亮的变量选择
【发布时间】:2014-07-23 16:35:11
【问题描述】:

我有这样的声明,可以让我获得关于我的变量的基本描述性统计数据:

checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
                          names(input_data), selected = names(input_data))

然而,在不得不取消单击 10 个变量以获取我感兴趣的一个变量之后,我意识到这个用户界面不是很友好。我想添加一个按钮,当您单击它时选择/取消选择全部。可以多次点击。我什至不知道如何开始。任何轻推都会有所帮助。

ui.R:

library(shiny)
hw<-diamonds 

shinyUI(fluidPage(
  title = 'Examples of DataTables',
  sidebarLayout(
    sidebarPanel(
        checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
                           names(hw), selected = names(hw))

    ),
    mainPanel(
      verbatimTextOutput("summary"), 
      tabsetPanel(
        id = 'dataset',
        tabPanel('hw', dataTableOutput('mytable1'))
        )
    )
  )
))

服务器.R:

library(shiny)
data(diamonds)
hw<-diamonds  
shinyServer(function(input, output) {
  output$summary <- renderPrint({
    dataset <- hw[, input$show_vars, drop = FALSE]
    summary(dataset)
  })
  # a large table, reative to input$show_vars
  output$mytable1 <- renderDataTable({
    library(ggplot2)
    hw[, input$show_vars, drop = FALSE]
  })
})

【问题讨论】:

  • 请提供其余代码 - server.Rui.R,以及测试程序所需的任何数据。
  • hw 原本是别的东西,所以我很快就为 diamonds 数据集整理了一些东西。
  • 为什么不直接使用selectize(multiple = TRUE) 而不是checkboxGroupInput()?您还将selected = 设置为names(...),这就是选择所有内容的原因 - 只需设置selected = '' 即可解决此问题。此解决方案不会让您选择“全部”选项(即使我正在寻找),但对于大约 10 种选择,我认为这是要走的路。可能是错的。

标签: r shiny


【解决方案1】:

这就是我设置全选/取消全选按钮的方式。

在 ui.R 中,在需要的地方添加一个操作按钮:

actionButton("selectall", label="Select/Deselect all")

然后server.R根据动作按钮的条件使用updateCheckboxGroupInput。如果按下按钮的次数是偶数则选择全部,否则如果是奇数则不选择。

# select/deselect all using action button

observe({
  if (input$selectall > 0) {
    if (input$selectall %% 2 == 0){
      updateCheckboxGroupInput(session=session, 
                               inputId="show_vars",
                               choices = list("carat" = "carat",
                                              "cut" = "cut",
                                              "color" = "color",
                                              "clarity"= "clarity",
                                              "depth" = "depth",
                                              "table" = "table",
                                              "price" = "price",
                                              "x" = "x",
                                              "y" = "y",
                                              "z" = "z"),
                               selected = c(names(hw)))

    } else {
      updateCheckboxGroupInput(session=session, 
                               inputId="show_vars",
                               choices = list("carat" = "carat",
                                              "cut" = "cut",
                                              "color" = "color",
                                              "clarity"= "clarity",
                                              "depth" = "depth",
                                              "table" = "table",
                                              "price" = "price",
                                              "x" = "x",
                                              "y" = "y",
                                              "z" = "z"),
                               selected = c())

    }}
})

以下示例的完整应用程序 - 您需要将会话添加到服务器函数,当没有选择任何变量时,我为 renderDataTable 添加了一个条件。

library(shiny)
library(ggplot2)
data(diamonds)
hw <- diamonds

runApp(
  list(
    ui=(
      fluidPage(
        title = 'Examples of DataTables',
        sidebarLayout(
          sidebarPanel(
            actionButton("selectall", label="Select/Deselect all"),
            checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
                               names(hw), selected = names(hw))

          ),
          mainPanel(
            verbatimTextOutput("summary"),
            tabsetPanel(
              id = 'dataset',
              tabPanel('hw', dataTableOutput('mytable1'))
            ))))),

    server = (function(input, output, session) {
      output$summary <- renderPrint({
        dataset <- hw[, input$show_vars, drop = FALSE]
        summary(dataset)
      })
      observe({
        if (input$selectall > 0) {
          if (input$selectall %% 2 == 0){
            updateCheckboxGroupInput(session=session, inputId="show_vars",
                                     choices = list("carat" = "carat",
                                                    "cut" = "cut",
                                                    "color" = "color",
                                                    "clarity"= "clarity",
                                                    "depth" = "depth",
                                                    "table" = "table",
                                                    "price" = "price",
                                                    "x" = "x",
                                                    "y" = "y",
                                                    "z" = "z"),
                                     selected = c(names(hw)))

          }
          else {
            updateCheckboxGroupInput(session=session, inputId="show_vars",
                                     choices = list("carat" = "carat",
                                                    "cut" = "cut",
                                                    "color" = "color",
                                                    "clarity"= "clarity",
                                                    "depth" = "depth",
                                                    "table" = "table",
                                                    "price" = "price",
                                                    "x" = "x",
                                                    "y" = "y",
                                                    "z" = "z"),
                                     selected = c())

          }}
      })

      # a large table, reative to input$show_vars
     output$mytable1 <- renderDataTable({
        if (is.null(input$show_vars)){
          data.frame("no variables selected" = c("no variables selected"))
        } else{
          hw[, input$show_vars, drop = FALSE]
        }

      })
    })

  ))

【讨论】:

  • 谢谢,这正是我所需要的,因为我不希望“全部”或“无”,而是“一些”作为默认值。
  • 不确定shiny 中是否发生了变化,但在此评论中selected = "" 可以取消选择所有内容,而selected = c() 则不会
【解决方案2】:

shinyWidgets 库有一个名为pickerInput() 的不错的函数,它带有“全选/取消全选”功能。经过大量研究,我发现这是唯一内置此功能的 Shiny 输入:

网站链接:https://dreamrs.github.io/shinyWidgets/index.html

【讨论】:

    【解决方案3】:

    我添加了一个 global.R 用于加载包和数据 - 并不总是必要的,但它通常更干净。可能有不同的方法来执行我在下面所做的事情,但我倾向于在这种情况下使用条件面板。

    ui.R

    library(shiny)
    
    shinyUI(fluidPage(
      title = 'Examples of DataTables',
      sidebarLayout(
        sidebarPanel(
    
          radioButtons(
            inputId="radio",
            label="Variable Selection Type:",
            choices=list(
              "All",
              "Manual Select"
            ),
            selected="All"),
    
          conditionalPanel(
            condition = "input.radio != 'All'",
            checkboxGroupInput(
              'show_vars', 
              'Columns in diamonds to show:',
              choices=names(hw), 
              selected = "carat"
            )
          )
    
        ),
        mainPanel(
          verbatimTextOutput("summary"), 
          tabsetPanel(
            id = 'dataset',
            tabPanel('hw', dataTableOutput('mytable1'))
          )
        )
      )
    ))
    

    服务器.R

    library(shiny)
    library(ggplot2)
    ##
    shinyServer(function(input, output) {
    
      Data <- reactive({
    
        if(input$radio == "All"){
          hw
        } else {
          hw[,input$show_vars,drop=FALSE]
        }
    
      })
    
      output$summary <- renderPrint({
        ## dataset <- hw[, input$show_vars, drop = FALSE]
        dataset <- Data()
        summary(dataset)
      })
    
      # a large table, reative to input$show_vars
      output$mytable1 <- renderDataTable({
        Data()
        ## hw[, input$show_vars, drop = FALSE]
      })
    })
    

    global.R

    library(shiny)
    library(ggplot2)
    data(diamonds)
    hw <- diamonds
    

    【讨论】:

    • 这不是我的想法,但它给了我一个想法,即创建一个名为“全选”的按钮,然后可以转到两个条件面板,一个选择所有条件面板,一个其他是空的。感谢您的启发!
    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2013-05-22
    相关资源
    最近更新 更多