运行R shinydashboard出现tagAssert函数body参数缺失报错如何解决
问题根因
你遇到的报错本质是dashboardBody代码存在语法错误和结构不规范问题,导致R无法正常解析出完整的dashboardBody对象,调用dashboardPage时就会判定body参数缺失,抛出对应报错。具体问题如下:
- 所有嵌套组件的闭合括号缺失:你贴出的代码末尾缺少6个右括号,分别对应未闭合的
tabPanel("Standard Deviation")、tabBox(id="tabs2")、tabPanel("Mean")、tabBox(id="tabs1")、column(width=6)、dashboardBody()层级的闭合符号 - 结构不符合shinydashboard规范:
tabItem不能直接和column平级放在fluidRow下,所有tabItem必须包裹在tabItems()容器中,如果你需要同时存在全局固定内容和tab切换内容,要把全局内容放在tabItems()之外单独嵌套 - 嵌套逻辑错误:你将第二个
tabBox(id="tabs2")直接作为tabPanel("Mean")的唯一参数,既没有关闭第一个tabPanel也没有补充其他tab选项,组件结构完全混乱
修复后可运行的dashboardBody代码
dashboardBody( # 全局非切换内容 fluidRow( column(width=6, box( title = "Select a Number", solidHeader = TRUE, background = "yellow", status="warning", width = NULL, height = 312, sliderInput(inputId = "number", label = "", value = 500, min = 25, max = 1000)), box(title = "Histogram", solidHeader=TRUE, background = "light-blue", status="primary", width = NULL, plotOutput("hist", height = 250)), valueBoxOutput("meanBox"), valueBoxOutput("medianBox"), valueBoxOutput("sdBox") ), column(width = 6, tabBox( title = "Central Tendency", id = "tabs1", height = 120, width = NULL, tabPanel("Mean", h2(textOutput("meantext"))), tabPanel("Median", h2(textOutput("mediantext"))) ), tabBox( title = "Variability", id = "tabs2",height = 120, width = NULL, side = "right", tabPanel("Variance", h2(textOutput("vartext")),width = NULL), tabPanel("Standard Deviation", h2(textOutput("sdtext")), width = NULL) ) ) ), # tab切换内容容器 tabItems( tabItem( tabName = "normal", fluidRow( box(title = "select a Number", solidHeader = TRUE, collapsible = TRUE, status = "warning", sliderInput(inputId = "normnumber", label = "", value = 500,min = 25, max = 1000)), box(title = "Density Plot", solidHeader = TRUE, background = "light-blue", status = "primary", plotOutput("density", height = 250)), infoBoxOutput("meanInfoBox"), infoBoxOutput("medianInfoBox"), infoBoxOutput("sdInfoBox") ) ) ) )
额外排查要点
- 写完嵌套组件后可以通过RStudio的括号匹配功能(点击括号会高亮对应配对括号)检查是否有漏写的闭合符号
- 确认
dashboardPage的三个参数dashboardHeader()、dashboardSidebar()、dashboardBody()都完整传入,没有拼写错误 - 侧边栏的
menuItem对应的tabName要和tabItem的tabName完全一致,大小写敏感
内容的提问来源于stack exchange,提问作者Zach Paiz
相关产品推荐
相关产品推荐

