
本文将指导你如何使用 Shiny 和 Sortable.js 库创建一个具有固定高度和滚动条的 bucket list。通过添加 CSS 样式来限制容器高度,并设置 overflow 属性,即可实现当列表项过多时,在容器右侧显示滚动条的效果。
以下步骤将详细介绍如何修改现有的 Shiny 应用,使其 bucket list 容器具有固定的高度和滚动条。
1. 修改 UI 代码
首先,需要修改 ui.R 文件(或者 app.R 文件中的 UI 部分),为 bucket list 容器添加额外的 div 包装器,并添加相应的 CSS 样式。
library(shiny)
library(sortable)
ui <- fluidPage(
tags$head(
tags$style(HTML(".bucket-list-container {min-height: 350px;}"))
),
fluidRow(
column(
width = 12,
#choose list of variable names to send to bucket list
radioButtons(inputId="variableList",
label="Choose your variable list",
choices = c("names(mtcars)"="names(mtcars)","state.name"="state.name")),
#input text to subset variable names
textInput(
inputId = "subsetChooseListText",
label = "Input text to subset list of states to choose from",
value = "c"
),
div(
# class value is current default class value for container
class = "bucket-list-container default-sortable",
"Drag the items in any desired bucket",
# 添加wrapper div
div(style = "width: 100%; height: 200px; overflow-y: auto;",
# class value is current default class value for list
div(
class = "default-sortable bucket-list bucket-list-horizontal",
# need to make sure the outer div size is respected
# use the current default flex value
uiOutput("selection_list", style="flex:1 0 200px;"),
rank_list(
text = "to here",
labels = list(),
input_id = "rank_list_2",
options = sortable_options(group = "mygroup")
),
rank_list(
text = "and also here",
labels = list(),
input_id = "rank_list_3",
options = sortable_options(group = "mygroup")
)
)
)
)
)
),
fluidRow(
column(
width = 12,
tags$b("Result"),
column(
width = 12,
tags$p("input$rank_list_1"),
verbatimTextOutput("results_1"),
tags$p("input$rank_list_2"),
verbatimTextOutput("results_2"),
tags$p("input$rank_list_3"),
verbatimTextOutput("results_3")
)
)
)
)
server <- function(input,output) {
#initialize reactive values
varList <- reactive({
req(input$variableList)
if (input$variableList == "state.name") {
state.name
} else {
paste0(rep(names(mtcars), 20),"_", 1:220)
}
})
subsetChooseList <- reactive({
items <- varList()
pattern <- input$subsetChooseListText
if (nchar(pattern) < 1) {
return(items)
}
items[
grepl(
x = items,
pattern = input$subsetChooseListText,
ignore.case = TRUE
)
]
})
output$selection_list <- renderUI({
labels <- subsetChooseList()
# remove already chosen items
labels <- labels[!(
labels %in% input$rank_list_2 |
labels %in% input$rank_list_3
)]
rank_list(
text = "Drag from here",
labels = labels,
input_id = "rank_list_1",
options = sortable_options(group = "mygroup")
)
})
#visual output for debugging
output$results_1 <- renderPrint(input$rank_list_1)
output$results_2 <- renderPrint(input$rank_list_2)
output$results_3 <- renderPrint(input$rank_list_3)
}
shinyApp(ui, server)代码解释:
2. 运行应用
保存修改后的代码,并重新运行 Shiny 应用。现在,如果 Drag from here 列表中的项目数量超过容器的高度(200px),就会在容器右侧显示一个垂直滚动条,允许用户滚动浏览所有列表项。
注意事项:
总结:
通过简单的 CSS 样式设置,就可以轻松地为 Shiny 应用中的 bucket list 添加滚动条,从而提高用户体验,尤其是在处理大量列表项时。关键在于使用一个 div 元素包裹 rank_list,并设置 overflow-y: auto;,同时指定一个合适的 height 值。
以上就是使用 Shiny 和 Sortable 创建可滚动 Bucket List的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号