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

Shiny Dashboard服务端循环生成box出现多余div文本该如何解决?

多余文本产生原因

你看到的div col-sm-1文本是因为错误使用c()函数拼接Shiny UI组件导致的:
Shiny的column()、box()等UI函数返回的是结构化的tag对象,不是普通字符。使用基础函数c()拼接多个tag对象时,会把tag的HTML类名、属性等内容意外展开为普通文本输出到页面,文本数量和你生成的box数量完全对应。

修复方案

只需要修改server.R中存储UI元素的容器类型和赋值方式即可,原有逻辑无需调整:

  • 将初始化的空容器从boxlist = c()改为boxlist = list(),列表是Shiny官方指定的多UI组件存储容器
  • 循环中追加元素时,用列表下标赋值boxlist[[i]]代替c()拼接
修复后的完整server.R代码
library(shiny)
library(shinydashboard)

shinyServer(function(input, output) {

    output$boxes <- renderUI({
        
        boxlist = list()
        for(i in 1:input$numberOfBoxes) {
            ddmenu1 <- selectInput(paste0("ddmenu1_in_box",i), "Animal", list('cat', 'dog', 'rabbit'))
            ddmenu2 <- selectInput(paste0("ddmenu2_in_box",i), "Color", list('red', 'blue', 'green'))
            button <- actionButton(paste0("justabutton_in_box",i), "Click me!")
            boxlist[[i]] <- column(1, box(ddmenu1, ddmenu2, button))
        }
        
        boxlist

    })

})

修改后即可正常生成指定数量的box,不会再出现多余文本。

内容的提问来源于stack exchange,提问作者Aku-Ville Lehtimäki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 07:06:02