r - Show total number of boxes checked NEXT to ActionButton
问题描述
I have a column of checkboxes. I would like to add the running total of the number of boxes checked and have it displayed next to (not beneath) the action button. Something like:
Options checked: <num_states_checked> (Max = 10)
How do I do this?
library(shiny)
library(tidyverse)
df <- tibble(
state = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida"),
abr = c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL")
)
selected_states <- c("AL", "AK","DE")
ui <- fluidPage(
wellPanel(
tags$label("Choose :"),
fluidRow(
column(
width = 4,
checkboxGroupInput(
selected = df$abr[1:3],
inputId = "checka",
label = NULL,
choiceNames = df$state,
choiceValues = df$abr
#choices = df$state
),
actionButton("update", "Update", class = "btn-primary", width = 250,
style="color: #FFF; background-color: #525252;"),
# textOutput("selected"),
hr()
),
)
)
)
server <- function(input, output, session) {
observeEvent(input$all_none,{
updateCheckboxGroupInput(
session, 'checka', choiceNames = df$state, choiceValues = df$abr,
selected = if (input$all_none == TRUE) df$state
)
}, ignoreInit = 1)
output$selected <- renderText({
all_selected <- paste(c(input$checka), collapse = ", ")
})
}
shinyApp(ui, server)
解决方案
您可以尝试使用 splitLayout。
library(shiny)
library(tidyverse)
df <- tibble(
state = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida"),
abr = c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL")
)
selected_states <- c("AL", "AK","DE")
ui <- fluidPage(
wellPanel(
tags$label("Choose :"),
fluidRow(
column(
width = 4,
checkboxGroupInput(
selected = df$abr[1:3],
inputId = "checka",
label = NULL,
choiceNames = df$state,
choiceValues = df$abr
#choices = df$state
)
),
),
splitLayout(cellWidths = c(250,"50%"),
actionButton("update", "Update", class = "btn-primary", width = 250,
style="color: #FFF; background-color: #525252;"),
h4(textOutput("selected"))
),
hr()
)
)
server <- function(input, output, session) {
observeEvent(input$all_none,{
updateCheckboxGroupInput(
session, 'checka', choiceNames = df$state, choiceValues = df$abr,
selected = if (input$all_none == TRUE) df$state
)
}, ignoreInit = 1)
output$selected <- renderText({
all_selected <- paste0("Options checked:", length(c(input$checka)), " (Max = ", length(df$state) , ")")
})
}
shinyApp(ui, se
rver)
推荐阅读
- swift - 无法将“Void”类型的值转换为预期的参数类型“() -> Void”
- reactjs - 反应路由器 v6 history.listen
- python - Jupyter Notebook 会创建一个 .txt 文件 - 并且不允许我另存为 .ipynb。手动创建 .ipynb 文件不会打开 jupyter notebook
- javascript - 使用 ref 时哪个重载会匹配调用?
- flutter - Flutter相机拍照时间太长
- pine-script - 不能用 'expr1'=series[bool] 调用 'operator >='。参数的类型应该是: const float
- java - 运行应用程序时 gRPC 依赖项中的重复类错误
- sql - 使用存储过程插入表中
- excel - 颜色数据透视表数据取决于列标题与行数据相比
- c# - 同时运行两个不同的任务来修改它们的优先级