shiny-bslib
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseModern Shiny Apps with bslib
基于bslib的现代化Shiny应用
Build professional Shiny dashboards using bslib's Bootstrap 5 components and layouts. This skill focuses on modern UI/UX patterns that replace legacy Shiny approaches.
使用bslib的Bootstrap 5组件与布局构建专业级Shiny仪表盘。本技能聚焦于可替代传统Shiny方案的现代化UI/UX模式。
Quick Start
快速开始
Single-page dashboard:
r
library(shiny)
library(bslib)
ui <- page_sidebar(
title = "My Dashboard",
theme = bs_theme(version = 5), # "shiny" preset by default
sidebar = sidebar(
selectInput("variable", "Variable", choices = names(mtcars))
),
layout_column_wrap(
width = 1/3,
fill = FALSE,
value_box(title = "Users", value = "1,234", theme = "primary"),
value_box(title = "Revenue", value = "$56K", theme = "success"),
value_box(title = "Growth", value = "+18%", theme = "info")
),
card(
full_screen = TRUE,
card_header("Plot"),
plotOutput("plot")
)
)
server <- function(input, output, session) {
output$plot <- renderPlot({
hist(mtcars[[input$variable]], main = input$variable)
})
}
shinyApp(ui, server)Multi-page dashboard:
r
ui <- page_navbar(
title = "Analytics Platform",
theme = bs_theme(version = 5),
nav_panel("Overview", overview_ui),
nav_panel("Analysis", analysis_ui),
nav_panel("Reports", reports_ui)
)单页仪表盘:
r
library(shiny)
library(bslib)
ui <- page_sidebar(
title = "My Dashboard",
theme = bs_theme(version = 5), # "shiny" preset by default
sidebar = sidebar(
selectInput("variable", "Variable", choices = names(mtcars))
),
layout_column_wrap(
width = 1/3,
fill = FALSE,
value_box(title = "Users", value = "1,234", theme = "primary"),
value_box(title = "Revenue", value = "$56K", theme = "success"),
value_box(title = "Growth", value = "+18%", theme = "info")
),
card(
full_screen = TRUE,
card_header("Plot"),
plotOutput("plot")
)
)
server <- function(input, output, session) {
output$plot <- renderPlot({
hist(mtcars[[input$variable]], main = input$variable)
})
}
shinyApp(ui, server)多页仪表盘:
r
ui <- page_navbar(
title = "Analytics Platform",
theme = bs_theme(version = 5),
nav_panel("Overview", overview_ui),
nav_panel("Analysis", analysis_ui),
nav_panel("Reports", reports_ui)
)Core Concepts
核心概念
Page Layouts
页面布局
- -- Single-page dashboard with sidebar (most common)
page_sidebar() - -- Multi-page app with top navigation bar
page_navbar() - -- Viewport-filling layout for custom arrangements
page_fillable() - -- Scrolling layout for long-form content
page_fluid()
See page-layouts.md for detailed guidance.
- -- 带侧边栏的单页仪表盘(最常用)
page_sidebar() - -- 带顶部导航栏的多页应用
page_navbar() - -- 可填充视口的自定义布局
page_fillable() - -- 适用于长内容的滚动布局
page_fluid()
详见page-layouts.md获取详细指导。
Grid Systems
网格系统
- -- Uniform grid with auto-wrapping (recommended for most cases)
layout_column_wrap() - -- 12-column Bootstrap grid with precise control
layout_columns()
See grid-layouts.md for detailed guidance.
- -- 支持自动换行的统一网格(大多数场景推荐使用)
layout_column_wrap() - -- 可精确控制的12列Bootstrap网格
layout_columns()
详见grid-layouts.md获取详细指导。
Cards
卡片
Primary container for dashboard content. Support headers, footers, multiple body sections, and full-screen expansion.
See cards.md for detailed guidance.
仪表盘内容的主要容器,支持页眉、页脚、多主体区域以及全屏展开功能。
详见cards.md获取详细指导。
Value Boxes
数值框
Display key metrics and KPIs with optional icons, sparklines, and built-in theming.
See value-boxes.md for detailed guidance.
展示关键指标与KPI,支持可选图标、迷你折线图及内置主题。
详见value-boxes.md获取详细指导。
Navigation
导航
- Page-level: for multi-page apps
page_navbar() - Component-level: ,
navset_card_underline(),navset_tab()for tabbed contentnavset_pill()
See navigation.md for detailed guidance.
- 页面级:用于多页应用
page_navbar() - 组件级:、
navset_card_underline()、navset_tab()用于标签页内容navset_pill()
详见navigation.md获取详细指导。
Sidebars
侧边栏
- Page-level: or
page_sidebar()page_navbar(sidebar = ...) - Component-level: within cards
layout_sidebar() - Supports conditional content, dynamic open/close, accordions
See sidebars.md for detailed guidance.
- 页面级:或
page_sidebar()page_navbar(sidebar = ...) - 组件级:卡片内的
layout_sidebar() - 支持条件内容、动态展开/折叠、折叠面板
详见sidebars.md获取详细指导。
Filling Layouts
填充布局
The fill system controls how components resize to fill available space. Key concepts: fillable containers, fill items, fill carriers. Fill activates when containers have defined heights.
See filling.md for detailed guidance.
填充系统控制组件如何调整大小以填充可用空间,核心概念包括可填充容器、填充项、填充载体。当容器定义高度时,填充功能激活。
详见filling.md获取详细指导。
Theming
主题设置
- with Bootswatch themes for quick styling
bs_theme() - Custom colors: ,
bg,fgaffect hundreds of CSS rulesprimary - Fonts: for typography
font_google() - Dynamic theming: +
input_dark_mode()session$setCurrentTheme()
See theming.md for detailed guidance.
- 结合Bootswatch主题实现快速样式设置
bs_theme() - 自定义颜色:、
bg、fg可影响数百条CSS规则primary - 字体:用于排版设置
font_google() - 动态主题:+
input_dark_mode()session$setCurrentTheme()
详见theming.md获取详细指导。
UI Components
UI组件
- Accordions -- Collapsible sections, especially useful in sidebars
- Tooltips -- Hover-triggered help text
- Popovers -- Click-triggered containers for secondary UI/inputs
- Toasts -- Temporary notification messages
See accordions.md, tooltips-popovers.md, and toasts.md.
- 折叠面板 -- 可折叠区域,在侧边栏中尤为实用
- 工具提示 -- 悬停触发的帮助文本
- 弹出框 -- 点击触发的次级UI/输入容器
- 通知提示 -- 临时通知消息
详见accordions.md、tooltips-popovers.md及toasts.md。
Icons
图标
Recommended: package (Bootstrap Icons, designed for bslib):
bsiconsr
bsicons::bs_icon("graph-up")
bsicons::bs_icon("people", size = "2em")Browse icons: https://icons.getbootstrap.com/
Alternative: package:
fontawesomer
fontawesome::fa("envelope")Accessibility for icon-only triggers: When an icon is used as the sole trigger for a tooltip, popover, or similar interactive element (no accompanying text), it must be accessible to screen readers. By default, icon packages mark icons as decorative (), which hides them from assistive technology.
aria-hidden="true"- : Provide
bsicons::bs_icon()— this automatically setstitlea11y = "sem"rtooltip( bs_icon("info-circle", title = "More information"), "Tooltip content here" ) - : Set
fontawesome::fa()and providea11y = "sem"titlertooltip( fa("circle-info", a11y = "sem", title = "More information"), "Tooltip content here" )
The should describe the purpose of the trigger (e.g., "More information", "Settings"), not the icon itself (e.g., not "info circle icon").
title推荐使用:包(为bslib设计的Bootstrap Icons):
bsiconsr
bsicons::bs_icon("graph-up")
bsicons::bs_icon("people", size = "2em")替代方案:包:
fontawesomer
fontawesome::fa("envelope")**纯图标触发器的可访问性:**当图标作为工具提示、弹出框或类似交互元素的唯一触发器(无伴随文本)时,必须支持屏幕阅读器访问。默认情况下,图标包会将图标标记为装饰性(),这会使其对辅助技术不可见。
aria-hidden="true"- :提供
bsicons::bs_icon()参数——这会自动设置titlea11y = "sem"rtooltip( bs_icon("info-circle", title = "更多信息"), "工具提示内容" ) - :设置
fontawesome::fa()并提供a11y = "sem"参数titlertooltip( fa("circle-info", a11y = "sem", title = "更多信息"), "工具提示内容" )
titleSpecial Inputs
特殊输入组件
- -- Toggle switch (modern checkbox alternative)
input_switch() - -- Dark mode toggle
input_dark_mode() - -- Button for long-running operations
input_task_button() - -- Code editor with syntax highlighting
input_code_editor() - -- Textarea with explicit submission
input_submit_textarea()
See inputs.md for detailed guidance.
- -- 切换开关(现代化复选框替代方案)
input_switch() - -- 深色模式切换器
input_dark_mode() - -- 用于长时间运行操作的按钮
input_task_button() - -- 带语法高亮的代码编辑器
input_code_editor() - -- 带显式提交功能的文本域
input_submit_textarea()
详见inputs.md获取详细指导。
Common Workflows
常见工作流
Building a Dashboard
构建仪表盘
- Choose page layout: (single-page) or
page_sidebar()(multi-page)page_navbar() - Add theme with (consider Bootswatch for quick start)
bs_theme() - Create sidebar with inputs for filtering/controls
- Add value boxes at top for key metrics (set on container)
fill = FALSE - Arrange cards with or
layout_column_wrap()layout_columns() - Enable on all visualization cards
full_screen = TRUE - Add for plot theming
thematic::thematic_shiny()
- 选择页面布局:(单页)或
page_sidebar()(多页)page_navbar() - 使用添加主题(可考虑Bootswatch快速上手)
bs_theme() - 创建包含筛选/控制输入的侧边栏
- 在顶部添加数值框展示关键指标(在容器上设置)
fill = FALSE - 使用或
layout_column_wrap()排列卡片layout_columns() - 为所有可视化卡片启用
full_screen = TRUE - 添加实现图表主题同步
thematic::thematic_shiny()
Modernizing an Existing App
改造现有应用
See migration.md for a complete mapping of legacy patterns to modern equivalents. Key steps:
- Replace with
fluidPage()orpage_sidebar()page_navbar() - Replace /
fluidRow()withcolumn()layout_columns() - Wrap outputs in
card(full_screen = TRUE) - Add
theme = bs_theme(version = 5) - Convert key metrics to components
value_box() - Replace with
tabsetPanel()navset_card_underline()
详见migration.md获取传统模式与现代方案的完整映射。核心步骤:
- 用或
page_sidebar()替换page_navbar()fluidPage() - 用替换
layout_columns()/fluidRow()column() - 将输出包裹在中
card(full_screen = TRUE) - 添加
theme = bs_theme(version = 5) - 将关键指标转换为组件
value_box() - 用替换
navset_card_underline()tabsetPanel()
Guidelines
指导原则
- Prefer bslib page functions (,
page_sidebar(),page_navbar(),page_fillable()) over legacy equivalents (page_fluid(),fluidPage())navbarPage() - Use or
layout_column_wrap()for grid layouts instead oflayout_columns()/fluidRow(), which don't support filling layoutscolumn() - Wrap outputs in when building dashboards -- full-screen expansion is a high-value feature
card(full_screen = TRUE) - Set on
fill = FALSEcontainers holding value boxes (they shouldn't stretch to fill height)layout_column_wrap() - Pin Bootstrap version: include or a preset theme
theme = bs_theme(version = 5) - Use in the server so base R and ggplot2 plots match the app theme
thematic::thematic_shiny() - Use responsive widths like in
width = "250px"for auto-adjusting columnslayout_column_wrap() - Group sidebar inputs with when sidebars have many controls
accordion() - See migration.md for mapping legacy Shiny patterns to modern bslib equivalents
- 优先使用bslib页面函数(、
page_sidebar()、page_navbar()、page_fillable())而非传统等价函数(page_fluid()、fluidPage())navbarPage() - **使用或
layout_column_wrap()**实现网格布局,而非不支持填充布局的layout_columns()/fluidRow()column() - 将输出包裹在中构建仪表盘——全屏展开是高价值功能
card(full_screen = TRUE) - 在承载数值框的容器上设置
layout_column_wrap()(数值框不应拉伸填充高度)fill = FALSE - 固定Bootstrap版本:包含或预设主题
theme = bs_theme(version = 5) - **在server中使用**使基础R图表与ggplot2图表匹配应用主题
thematic::thematic_shiny() - 使用响应式宽度,如在中设置
layout_column_wrap()实现列自动调整width = "250px" - 当侧边栏包含大量控件时,用对输入进行分组
accordion() - **查看migration.md**获取传统Shiny模式到现代bslib等价方案的映射
Avoid Common Errors
避免常见错误
- Avoid directly nesting containers.
card()functions are already cards;navset_card_*()content goes directly inside them without wrapping innav_panel()card() - Only use and
layout_columns()for laying out multiple elements. Single children should be passed directly to their container functions.layout_column_wrap() - Never nest functions. Only use one top-level page function per app.
page_*()
- 避免直接嵌套容器。
card()函数本身已是卡片;navset_card_*()内容应直接传入,无需包裹在nav_panel()中card() - 仅在布局多个元素时使用和
layout_columns()。单个子元素应直接传入其容器函数layout_column_wrap() - 切勿嵌套函数。每个应用仅使用一个顶级页面函数
page_*()
Reference Files
参考文档
- migration.md -- Legacy Shiny to modern bslib migration guide
- page-layouts.md -- Page-level layout functions and patterns
- grid-layouts.md -- Multi-column grid systems
- cards.md -- Card components and features
- value-boxes.md -- Value boxes for metrics and KPIs
- navigation.md -- Navigation containers and patterns
- sidebars.md -- Sidebar layouts and organization
- filling.md -- Fillable containers and fill items
- theming.md -- Basic theming (colors, fonts, Bootswatch). See shiny-bslib-theming skill for advanced theming
- accordions.md -- Collapsible sections and sidebar organization
- tooltips-popovers.md -- Hover tooltips and click-triggered popovers
- toasts.md -- Temporary notification messages
- inputs.md -- Special bslib input widgets
- best-practices.md -- bslib-specific patterns and common gotchas
- migration.md -- 传统Shiny到现代bslib的迁移指南
- page-layouts.md -- 页面级布局函数与模式
- grid-layouts.md -- 多列网格系统
- cards.md -- 卡片组件与特性
- value-boxes.md -- 用于指标与KPI的数值框
- navigation.md -- 导航容器与模式
- sidebars.md -- 侧边栏布局与组织
- filling.md -- 可填充容器与填充项
- theming.md -- 基础主题设置(颜色、字体、Bootswatch)。高级主题设置请查看shiny-bslib-theming技能
- accordions.md -- 可折叠区域与侧边栏组织
- tooltips-popovers.md -- 悬停工具提示与点击触发的弹出框
- toasts.md -- 临时通知消息
- inputs.md -- 特殊bslib输入组件
- best-practices.md -- bslib专属模式与常见陷阱