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

如何让renderUI()渲染的value_box不垂直堆叠?

问题解决:Quarto Dashboard中renderUI生成的Value Box并排显示

问题原因

Quarto Dashboard的布局默认会把同一标题区块下的value_box塞进flex布局容器,自动实现并排。但每个uiOutput会生成独立的<div>包裹容器,这个容器会打断原有的flex布局上下文,导致每个动态生成的value_box被单独的块级容器包裹,最终垂直堆叠。

解决方案

方案1:把多个Value Box放在同一个renderUI里

将需要并排的value_box统一放在一个renderUI函数中,这样它们会被渲染到同一个容器内,继承父级的flex布局规则,自然实现并排。

修改后的完整MRE代码:

---
title: "Value Box Layout Comparison"
format:
  dashboard:
    orientation: columns
    page-navigation: true
    page-navigation-id: site_nav
server: shiny
---

```{r}
#| context: setup
library(shiny)
library(bslib)

Nothing here — just a comparison between UI and server-based value boxes.

Value Boxes directly in UI

Three value boxes side-by-side {height="40%"}

value_box(
  title = "Box UI 1",
  value = 42,
  theme = "primary"
)

value_box(
  title = "Box UI 2",
  value = 58,
  theme = "primary"
)

value_box(
  title = "Box UI 3",
  value = 68,
  theme = "primary"
)

Three value boxes side-by-side {height="60%"}

#| component: input
#| title: Just space
cat("Just space")

Value Boxes rendered from server

Three value boxes via renderUI() {height="40%"}

#| title: Value Boxes rendered from server
uiOutput("server_value_boxes")

Three value boxes side-by-side {height="60%"}

#| component: input
#| title: Just space
cat("Just space")
#| context: server

output$server_value_boxes <- renderUI({
  tagList(
    value_box(
      title = "Box Server 1",
      value = 123,
      theme = "danger"
    ),
    value_box(
      title = "Box Server 2",
      value = 321,
      theme = "danger"
    ),
    value_box(
      title = "Box Server 3",
      value = 456,
      theme = "danger"
    )
  )
})
### 方案2:给uiOutput添加布局类
如果需要保留多个独立的`uiOutput`(比如每个value_box需要单独更新),可以给每个`uiOutput`加上Quarto Dashboard对应的布局类,让它们成为flex布局的子项。

修改UI部分的代码:
```{r}
#| title: Value Boxes rendered from server
uiOutput("box_server_1", class="col")
uiOutput("box_server_2", class="col")
uiOutput("box_server_3", class="col")

也可以用自定义CSS强制容器为行内块:

.value-box-container {
  display: inline-block;
  width: 33%;
  margin: 0;
}

然后给uiOutput指定类:

uiOutput("box_server_1", class="value-box-container")
uiOutput("box_server_2", class="value-box-container")
uiOutput("box_server_3", class="value-box-container")

多语言适配说明

两种方案都基于服务器端渲染,完全兼容shiny.i18n的语言切换需求——只需把renderUI里的title替换成多语言翻译函数(比如i18n$t("box_title_1"))即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 06:30:58