You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何放大bslib::value_box中的图标与文本(兼容htmlOutput)

放大bslib value_box图标与文本的实现方案

方法一:自定义CSS(灵活精确控制)

直接在UI中添加自定义CSS样式,针对value_box的标题、数值、图标元素设置更大的字体尺寸,完全兼容htmlOutput:

library(shiny)
library(bslib)

ui = page_fluid(
  titlePanel("Test App"),
  # 添加自定义放大样式
  tags$style(HTML("
    /* 放大value_box标题 */
    .value-box .value-box-title {
      font-size: 2rem; /* 原默认约1rem,放大2倍 */
    }
    /* 放大value_box数值内容 */
    .value-box .value-box-value {
      font-size: 3rem; /* 原默认约1rem,放大3倍 */
    }
    /* 放大展示区图标 */
    .value-box .value-box-showcase i {
      font-size: 4rem; /* 原默认约2rem,放大2倍 */
    }
  ")),
  
  fluidRow(
    selectInput("species_richness",
                "Select number of species",
                choices = c(1:5)),
    selectInput("mean_c",
                "Select mean C",
                choices = c(1:5)),
    selectInput("fqi",
                "Select FQI",
                choices = c(1:5))
  ),
  
  layout_column_wrap(
    width = 1/3,
    bslib::value_box(
      title = "Species Richness",
      value = htmlOutput("species_richness"),
      showcase = icon("seedling")
    ),
    bslib::value_box(
      title = "Mean C",
      value = htmlOutput("mean_c"),
      showcase = icon("pagelines")
    ),
    bslib::value_box(
      title = "Total FQI",
      value = htmlOutput("fqi"),
      showcase = icon("spa")
    )
  )
)

server = function(session, input, output) {
  output$species_richness <- renderUI({
    input$species_richness
  })
  output$mean_c <- renderUI({
    input$mean_c
  })
  output$fqi <- renderUI({
    input$fqi
  })
}

shinyApp(ui, server)

说明:

  • CSS选择器直接对应bslib value_box的默认类名,无需修改原有组件结构
  • 可根据需求调整font-size数值,精准控制放大倍数
  • 不影响服务器端渲染的内容展示

方法二:利用bslib主题变量(原生风格适配)

通过bslib的主题系统覆盖默认变量,贴合bslib原生设计逻辑,同样兼容htmlOutput:

library(shiny)
library(bslib)

ui = page_fluid(
  titlePanel("Test App"),
  # 配置bslib主题,覆盖value_box样式变量
  theme = bslib::bs_theme(
    variables = list(
      "value-box-title-font-size" = "2rem",
      "value-box-value-font-size" = "3rem",
      "value-box-showcase-icon-font-size" = "4rem"
    )
  ),
  
  fluidRow(
    selectInput("species_richness",
                "Select number of species",
                choices = c(1:5)),
    selectInput("mean_c",
                "Select mean C",
                choices = c(1:5)),
    selectInput("fqi",
                "Select FQI",
                choices = c(1:5))
  ),
  
  layout_column_wrap(
    width = 1/3,
    bslib::value_box(
      title = "Species Richness",
      value = htmlOutput("species_richness"),
      showcase = icon("seedling")
    ),
    bslib::value_box(
      title = "Mean C",
      value = htmlOutput("mean_c"),
      showcase = icon("pagelines")
    ),
    bslib::value_box(
      title = "Total FQI",
      value = htmlOutput("fqi"),
      showcase = icon("spa")
    )
  )
)

server = function(session, input, output) {
  output$species_richness <- renderUI({
    input$species_richness
  })
  output$mean_c <- renderUI({
    input$mean_c
  })
  output$fqi <- renderUI({
    input$fqi
  })
}

shinyApp(ui, server)

说明:

  • 使用bslib原生主题变量,样式更统一,避免自定义CSS可能的冲突
  • 变量名符合bslib内置设计规范,可扩展调整更多样式细节

内容的提问来源于stack exchange,提问作者ifoxfoot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:24:57