简体   繁体   English

闪亮的下拉复选框输入

[英]drop-down checkbox input in shiny

Is it possible to have a dropdown list in Shiny where you can select multiple values?是否可以在 Shiny 中有一个下拉列表,您可以在其中选择多个值? I know selectInput has the option to set multiple = T but I don't like it that all selected option are visible in the screen, especially since I have over 40. The same holds for checkboxGroupInput() , which I like more but still all selected values are shown.我知道selectInput有设置multiple = T的选项,但我不喜欢它所有选定的选项都在屏幕上可见,尤其是因为我有超过 40 个。同样适用于checkboxGroupInput() ,我更喜欢它但仍然是所有显示选定的值。 Isn't it just possible to get a drop-down like the one I copied from Excel below, rather than the examples of Shinys selectInput and checkboxGroupInput() thereafter?是不是可以得到一个下拉菜单,就像我从下面的 Excel 中复制的那样,而不是之后的 Shinys selectInputcheckboxGroupInput()的例子?

Excel 闪亮1 闪亮2 闪亮3

EDIT : This function (and others) is available in package shinyWidgets编辑:此功能(和其他功能)在包shinyWidgets中可用


Hi I wrote this dropdownButton function once, it create a bootstrap dropdown button (doc here ), the results looks like:您好,我曾经写过这个dropdownButton函数,它创建了一个引导下拉按钮(此处为文档),结果如下所示:

下拉按钮

Here is the code:这是代码:

# func --------------------------------------------------------------------

dropdownButton <- function(label = "", status = c("default", "primary", "success", "info", "warning", "danger"), ..., width = NULL) {

  status <- match.arg(status)
  # dropdown button content
  html_ul <- list(
    class = "dropdown-menu",
    style = if (!is.null(width)) 
      paste0("width: ", validateCssUnit(width), ";"),
    lapply(X = list(...), FUN = tags$li, style = "margin-left: 10px; margin-right: 10px;")
  )
  # dropdown button apparence
  html_button <- list(
    class = paste0("btn btn-", status," dropdown-toggle"),
    type = "button", 
    `data-toggle` = "dropdown"
  )
  html_button <- c(html_button, list(label))
  html_button <- c(html_button, list(tags$span(class = "caret")))
  # final result
  tags$div(
    class = "dropdown",
    do.call(tags$button, html_button),
    do.call(tags$ul, html_ul),
    tags$script(
      "$('.dropdown-menu').click(function(e) {
      e.stopPropagation();
});")
  )
  }

And an example:和一个例子:

# app ---------------------------------------------------------------------

library("shiny")
ui <- fluidPage(
  tags$h1("Example dropdown button"),
  br(),
  fluidRow(
    column(
      width = 6,
      dropdownButton(
        label = "Check some boxes", status = "default", width = 80,
        checkboxGroupInput(inputId = "check1", label = "Choose", choices = paste(1:26, ") Choice ", LETTERS))
      ),
      verbatimTextOutput(outputId = "res1")
    ),
    column(
      width = 6,
      dropdownButton(
        label = "Check some boxes", status = "default", width = 80,
        actionButton(inputId = "a2z", label = "Sort A to Z", icon = icon("sort-alpha-asc")),
        actionButton(inputId = "z2a", label = "Sort Z to A", icon = icon("sort-alpha-desc")),
        br(),
        actionButton(inputId = "all", label = "(Un)select all"),
        checkboxGroupInput(inputId = "check2", label = "Choose", choices = paste(1:26, ") Choice ", LETTERS))
      ),
      verbatimTextOutput(outputId = "res2")
    )
  )
)
server <- function(input, output, session) {
  output$res1 <- renderPrint({
    input$check1
  })

  # Sorting asc
  observeEvent(input$a2z, {
    updateCheckboxGroupInput(
      session = session, inputId = "check2", choices = paste(1:26, ") Choice ", LETTERS), selected = input$check2
    )
  })
  # Sorting desc
  observeEvent(input$z2a, {
    updateCheckboxGroupInput(
      session = session, inputId = "check2", choices = paste(26:1, ") Choice ", rev(LETTERS)), selected = input$check2
    )
  })
  output$res2 <- renderPrint({
    input$check2
  })
  # Select all / Unselect all
  observeEvent(input$all, {
    if (is.null(input$check2)) {
      updateCheckboxGroupInput(
        session = session, inputId = "check2", selected = paste(1:26, ") Choice ", LETTERS)
      )
    } else {
      updateCheckboxGroupInput(
        session = session, inputId = "check2", selected = ""
      )
    }
  })
}
shinyApp(ui = ui, server = server)

In bonus I put the ascending/descending sorting thingy in the second dropdown buttons.作为奖励,我将升序/降序排序放在第二个下拉按钮中。

EDIT Mar 22 '16编辑 16 年 3 月 22 日

To split yours checkboxes into multiple columns you can do the split yourself with fluidRow and columns and multiples checkboxes, you just have to bind the values server-side.要将您的复选框拆分为多列,您可以使用fluidRowcolumns以及多个复选框自行拆分,您只需在服务器端绑定值。 To implement scrolling put your checkboxes into a div with style='overflow-y: scroll; height: 200px;'要实现滚动,请将您的复选框放入带有style='overflow-y: scroll; height: 200px;'的 div 中。 style='overflow-y: scroll; height: 200px;' . .

Look at this example:看这个例子:

library("shiny")
ui <- fluidPage(
  tags$h1("Example dropdown button"),
  br(),
  fluidRow(
    column(
      width = 6,
      dropdownButton(
        label = "Check some boxes", status = "default", width = 450,
        tags$label("Choose :"),
        fluidRow(
          column(
            width = 4,
            checkboxGroupInput(inputId = "check1a", label = NULL, choices = paste0(1:10, ") ", LETTERS[1:10]))
          ),
          column(
            width = 4,
            checkboxGroupInput(inputId = "check1b", label = NULL, choices = paste0(11:20, ") ", LETTERS[11:20]))
          ),
          column(
            width = 4,
            checkboxGroupInput(inputId = "check1c", label = NULL, choices = paste0(21:26, ") ", LETTERS[21:26]))
          )
        )
      ),
      verbatimTextOutput(outputId = "res1")
    ),
    column(
      width = 6,
      tags$style(".container { border:2px solid steelblue; width: 100%; height: 200px; overflow-y: scroll; }"),
      dropdownButton(
        label = "Check some boxes", status = "default", width = 120,
        tags$div(
          class = "container",
          checkboxGroupInput(inputId = "check2", label = "Choose", choices = paste0(1:26, ") ", LETTERS))
        )
      ),
      verbatimTextOutput(outputId = "res2")
    )
  )
)
server <- function(input, output, session) {

  valuesCheck1 <- reactiveValues(x = NULL)
  observeEvent(input$check1a, valuesCheck1$x <- unique(c(valuesCheck1$x, input$check1a)))
  observeEvent(input$check1b, valuesCheck1$x <- unique(c(valuesCheck1$x, input$check1b)))
  observeEvent(input$check1c, valuesCheck1$x <- unique(c(valuesCheck1$x, input$check1c)))

  output$res1 <- renderPrint({
    valuesCheck1$x
  })

  output$res2 <- renderPrint({
    input$check2
  })

}
shinyApp(ui = ui, server = server)

Firstly, lot of thanks for this dropdownButton function.首先,非常感谢这个dropdownButton函数。 It's very useful!这非常有用!

Secondly, i tried to use it into shiny dashboard sidebarmenu, but the default characters' style is "color:white" (because of dark background).其次,我试图将它用于闪亮的仪表板侧边栏菜单,但默认字符的样式是“颜色:白色”(因为深色背景)。 That takes me a couple of hour to understand that can be changed inside your function, more precisly in html_ul stuff.我花了几个小时才明白可以在您的函数内部进行更改,更准确地说是在html_ul内容中。 Here's the line of interest, with color:black :这是感兴趣的行,颜色为:黑色

lapply(X = list(...), FUN = tags$li, style = "margin-left: 10px; margin-right: 10px; color:black")

Quite simple... But when you don't know it (R is the only language I know)... So, I hope this will help any other css-ignorant (and/or HTML?) like me!很简单......但是当你不知道它时(R 是我唯一知道的语言)......所以,我希望这会帮助任何其他像我一样无知的 css(和/或 HTML?)!

Cheers!干杯!

There are a couple questions in the comments related the the dropdownButton (worked great for me, thank you) about how to create a scrolling bar on the dropdown.在与 dropdownButton 相关的评论中有几个问题(对我来说非常有用,谢谢)关于如何在下拉菜单上创建滚动条 Sorry I don't have reputation to reply in the comments directly.对不起,我没有直接在评论中回复的声誉。

Try tweaking the relevant ID in your styles.css, for whatever object you put in the dropdownButton.尝试调整 styles.css 中的相关 ID,以适应您放入 dropdownButton 中的任何对象。 So for the example, the checkboxGroupInput ID needs to have:因此,对于示例,checkboxGroupInput ID 需要具有:

#check1
{
   height: 200px;
   overflow: auto;
}

Edit:编辑:

To call the styles.css in the ui.R:在 ui.R 中调用 styles.css:

navbarPage("Superzip", id="nav",

  tabPanel("Interactive map",
    div(class="outer",

      tags$head(
        # Include our custom CSS
        includeCSS("styles.css")
      ),

      leafletOutput("map", width="100%", height="100%"), 
      ...

And the styles.css, with the auto overflow for the inputID ttype and chain :和 styles.css,带有 inputID ttypechain的自动溢出:

input[type="number"] {


max-width: 80%;
}

div.outer {
  position: fixed;
  top: 41px;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  padding: 0;
}

/* Customize fonts */
body, label, input, button, select { 
  font-family: 'Helvetica Neue', Helvetica;
  font-weight: 200;
}
h1, h2, h3, h4 { font-weight: 400; }

#controls {
  /* Appearance */
  background-color: white;
  padding: 0 20px 20px 20px;
  cursor: move;
  /* Fade out while not hovering */
  opacity: 0.65;
  zoom: 0.9;
  transition: opacity 500ms 1s;
}
#controls:hover {
  /* Fade in while hovering */
  opacity: 0.95;
  transition-delay: 0;
}

#data_inputs {
  /* Appearance */
  background-color: white;
  padding: 0 20px 20px 20px;
  cursor: move;
  /* Fade out while not hovering */
  opacity: 0.65;
  zoom: 0.9;
  transition: opacity 500ms 1s;
}
#data_inputs:hover {
  /* Fade in while hovering */
  opacity: 0.95;
  transition-delay: 0;
}

/* Position and style citation */
#cite {
  position: absolute;
  bottom: 10px;
  left: 10px;
  font-size: 12px;
}

#cite {
  position: absolute;
  bottom: 10px;
  left: 10px;
  font-size: 12px;
}

#ttype
{
   height: 200px;
   overflow: auto;
}

#chain
{
   height: 200px;
   overflow: auto;
}



."form-group shiny-input-checkboxgroup shiny-input-container"
{
   height: 50px;
   overflow: auto;
}

/* If not using map tiles, show a white background */
.leaflet-container {
  background-color: white !important;
}

For future visitors that might need similar solutions, a good option could be the selectizeInput .对于可能需要类似解决方案的未来访问者,一个不错的选择可能是selectizeInput

Pros:优点:

  1. You can set the list length您可以设置列表长度
  2. Is a dropdown function是一个下拉函数
  3. User can select one or more choices by searching the list or by typing in the box.用户可以通过搜索列表或在框中键入来选择一个或多个选项。

For more information check the above link.有关更多信息,请查看上面的链接。 Hope this will help.希望这会有所帮助。

Cheers!干杯!

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM