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

在Shiny Dashboard中基于变量动态添加tabPanel时报错如何解决

问题原因

核心问题是重复嵌套了tabsetPanel组件:
dashboardBody中已经声明了一层tabsetPanel,但是你在该组件内部又用do.call(tabsetPanel, ...)生成了第二层tabsetPanel,两层组件的参数和子元素结构冲突,导致Shiny在设置默认选中标签时找不到对应元素,触发了属性赋值到NULL的报错。
另外你原代码里tags$img的高度属性拼写有误,heigth可修正为height,该问题不影响核心运行但会导致图片高度设置不生效。


修正后可正常运行的代码

library(shinydashboard)
library(tidyverse)

form <- 2

ui <- dashboardPage(
  title = "Rolê de Aventura", skin="blue",
  dashboardHeader(titleWidth = 1024,
                  title=list(title=tags$img(src="LogoPQ.png",
                                            height=45, width=45,
                                            align="left"),
                             title=tags$p(style="text-align:center;",
                                          "Rolê de Aventura")
                  )
  ),
  dashboardSidebar(
    selectInput("categoria", label = "Categoria:",
                choices = list("Quarteto Misto",
                               "Dupla Masculina",
                               "Dupla Mista",
                               "Dupla Feminina"), width="200px"
    )
  ),
  dashboardBody(
    textInput("equipe", "Nome da equipe:", width = NULL),
    # 仅保留一层tabsetPanel,直接传入动态生成的tabPanel列表
    do.call(tabsetPanel,c(width=NULL, type="tabs",
                          lapply(1:form, function(i) {
                            tabPanel(title = str_c("Atleta", i),
                                     textInput(str_c("atleta", i), "Nome:", width=NULL),
                                     dateInput(str_c("at.nasc", i), "Nascimento:", width="30%"),
                                     checkboxGroupInput(str_c("at.sex", i), "Sexo:", width="30%",
                                                        choices=list("Masculino", "Feminino")))
                          })
    )
  )
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

内容的提问来源于stack exchange,提问作者Vitor Hugo Moreau

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 15:15:03