R Shiny仪表板开发报错求助:tagAssert与object 'ui' not found错误排查
解决你的R Shiny仪表板错误问题
我来帮你排查这两个错误并修复代码:
错误原因分析
1. Error in force(ui) : object 'ui' not found
这个错误通常是因为代码中存在语法或逻辑错误,导致ui对象没有被正确创建。结合第二个错误来看,核心问题出在数据处理和响应式对象的调用上,间接导致dashboardPage无法生成有效的ui结构。
2. Error in tagAssert(body, type = "div", class = "content-wrapper")
这个错误说明dashboardBody的结构不符合shinydashboard的要求,本质是后续数据处理的错误影响了整个页面组件的渲染,同时你的代码中还存在列名拼写不一致、响应式对象调用错误等问题。
具体修复步骤
步骤1:修正列名拼写错误
你的代码里存在列名不一致的问题:
selectInput中用的是df1$Date_o_fTest- 但
filter中用的是Date_of_Test
这会导致筛选后的数据框为空,进而引发后续错误。统一列名(假设你的CSV文件中列名是Date_of_Test):
selectInput(inputId = "Date", label = "Select Date :", choices = unique(df1$Date_of_Test),multiple = T)
步骤2:正确调用响应式对象
dfsub是一个响应式对象,调用时必须加上(),不能直接用dfsub[5]。同时直接用索引取列容易出错,建议用列名,还要处理多选日期的情况(比如取最大值或均值):
删除原来错误的数值提取代码:
# 删掉这三行错误代码 Loadvalue <- dfsub[5] Tempvalue <- dfsub[6] Pressurevalue <- dfsub[7]
替换为带响应式调用和数据校验的代码:
output$value1 <- renderValueBox({ req(dfsub()) # 确保数据存在再执行 Loadvalue <- max(dfsub()$Load_Cell_N, na.rm = TRUE) # 取最大推力值 valueBox(formatC(Loadvalue, format = "f", digits = 2), 'Thrust (in N)', color = "aqua") }) output$value2 <- renderValueBox({ req(dfsub()) Tempvalue <- max(dfsub()$Baro_Temp_C, na.rm = TRUE) # 取最高温度 valueBox(formatC(Tempvalue, format = "f", digits = 2), 'Temperature (in °C)', color = "blue") }) output$value3 <- renderValueBox({ req(dfsub()) Pressurevalue <- max(dfsub()$Baro_Pressure_hpa, na.rm = TRUE) # 取最高压力 valueBox(formatC(Pressurevalue, format = "f", digits = 2), 'Pressure (in hPa)', color = "green") })
步骤3:补充缺失的Summary TabItem
你的sidebar里第一个菜单是tabName = "Summary",但body里没有对应的tabItem,点击会显示空白,建议补上:
body = dashboardBody( tabItems( # 第一个tab内容(Dashboard) tabItem(tabName = "Summary", h2("Overall Test Summary"), fluidRow( box(title = "Dataset Overview", width = 12, DT::dataTableOutput("data_overview")) ) ), # second tab content tabItem(tabName = "Daywise", h2("Test Day for the Day"), fluidRow(valueBoxOutput("value1",width=3), valueBoxOutput("value2",width=3), valueBoxOutput("value3",width=3)), ), # third tab content tabItem(tabName = "Model", h2("Simulation Summary"), fluidRow( box(title = "Thrust Curve", width = 8,solidHeader = TRUE, collapsible = TRUE, plotlyOutput("plot1",height=250)), ), fluidRow( box(title = "Temperature Curve", width = 4,solidHeader = TRUE, collapsible = TRUE, plotlyOutput("plot2",height=250)), box(title = "Pressure curve", width = 4,solidHeader = TRUE, collapsible = TRUE, plotlyOutput("plot3",height=250)), ) ), ))
同时在server里添加对应的输出:
output$data_overview <- DT::renderDataTable({ DT::datatable(df1, options = list(pageLength = 10)) })
步骤4:补充缺失的依赖包
filter函数来自dplyr,你没有加载这个包,需要添加:
library(dplyr)
完整修复后的代码
rm(list=ls()) ## app.R ## library(shiny) library(shinydashboard) library(shinydashboardPlus) library(ggplot2) library(plotly) library(caTools) library(caret) library(Hmisc) library(data.table) library(DT) library(reshape2) library(dplyr) # 添加dplyr包,用于filter函数 #Estes E9-4 setwd("E:/akshaya/courses/STAR Space/Internship/Team work/Dashboard") df1 = read.csv("Motor Test Data.csv",stringsAsFactors = F) # View(df1) # 建议注释掉,避免运行时弹出窗口 # RShiny dashboard header = dashboardHeader(title = "Solid Motor Test Results") sidebar = dashboardSidebar(sidebarMenu(id = "tabs", menuItem("Dashboard", tabName = "Summary", icon = icon("dashboard")), menuItem("Daywise", tabName = "Daywise", icon = icon("chart-line")), #to get input for a particular tab use conditional panel conditionalPanel( "input.tabs == 'Daywise'", #country selection selectInput(inputId = "Date", label = "Select Date :", choices = unique(df1$Date_of_Test),multiple = T) ), menuItem("Simulation Summary", tabName = "Model", icon = icon("chart-bar")), # 修正拼写Simmulation为Simulation menuItem("Visit-us", icon = icon("send",lib='glyphicon'),href = "https://www.starlabsurat.com/") ) ) body = dashboardBody( tabItems( # 第一个tab内容(Dashboard) tabItem(tabName = "Summary", h2("Overall Test Summary"), fluidRow( box(title = "Dataset Overview", width = 12, DT::dataTableOutput("data_overview")) ) ), # second tab content tabItem(tabName = "Daywise", h2("Test Day for the Day"), fluidRow(valueBoxOutput("value1",width=3), valueBoxOutput("value2",width=3), valueBoxOutput("value3",width=3)), ), # third tab content tabItem(tabName = "Model", h2("Simulation Summary"), fluidRow( box(title = "Thrust Curve", width = 8,solidHeader = TRUE, collapsible = TRUE, plotlyOutput("plot1",height=250)), ), fluidRow( box(title = "Temperature Curve", width = 4,solidHeader = TRUE, collapsible = TRUE, plotlyOutput("plot2",height=250)), box(title = "Pressure curve", width = 4,solidHeader = TRUE, collapsible = TRUE, plotlyOutput("plot3",height=250)), ) ), )) ui = dashboardPage(header, sidebar, body) server = function(input, output,session) { #for Simulation Summary output$plot1 = renderPlotly({ ggplotly(ggplot(df1, aes(x=Test_Time_sec, y=Load_Cell_N))+ geom_line(size = 1.2,color="red") ) }) output$plot2 = renderPlotly({ ggplotly(ggplot(df1, aes(x=Test_Time_sec, y=Baro_Temp_C))+ geom_line(size = 1.2,color="blue") ) }) output$plot3 = renderPlotly({ ggplotly(ggplot(df1, aes(x=Test_Time_sec, y=Baro_Pressure_hpa))+ geom_line(size = 1.2,color="green") ) }) #for Day wise tab dfsub <- reactive({ req(input$Date) # 确保输入存在再筛选 filter(df1,Date_of_Test %in% input$Date) }) # Dashboard Overview output$data_overview <- DT::renderDataTable({ DT::datatable(df1, options = list(pageLength = 10)) }) # Daywise Value Boxes output$value1 <- renderValueBox({ req(dfsub()) Loadvalue <- max(dfsub()$Load_Cell_N, na.rm = TRUE) valueBox(formatC(Loadvalue, format = "f", digits = 2), 'Thrust (in N)', color = "aqua") }) output$value2 <- renderValueBox({ req(dfsub()) Tempvalue <- max(dfsub()$Baro_Temp_C, na.rm = TRUE) valueBox(formatC(Tempvalue, format = "f", digits = 2), 'Temperature (in °C)', color = "blue") }) output$value3 <- renderValueBox({ req(dfsub()) Pressurevalue <- max(dfsub()$Baro_Pressure_hpa, na.rm = TRUE) valueBox(formatC(Pressurevalue, format = "f", digits = 2), 'Pressure (in hPa)', color = "green") }) } shinyApp(ui = ui, server = server)
额外说明
- 我修正了
Simmulation的拼写为Simulation,让菜单名称更规范。 - 添加了
req()函数,确保在输入或数据不存在时不会报错,提升应用的健壮性。 - 给数值显示添加了
digits=2,让输出更美观。
现在运行这个代码,应该就能正常启动应用,不会再出现之前的错误了。
内容的提问来源于stack exchange,提问作者Padmasree
相关产品推荐
相关产品推荐

