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

基于shinydashboard:动态日期范围过滤与ActiveEnergy可视化开发

没问题,我帮你写好了完整的shinydashboard实现代码,完全匹配你的需求:在「Historic」子菜单下通过日期范围选择器过滤数据,聚合后生成ActiveEnergy的条形图。下面是详细代码和说明:

完整实现代码

# 加载所需包
library(shinydashboard)
library(shiny)
library(ggplot2)
library(dplyr)

# 定义你的样本数据框(可替换为真实数据)
df <- data.frame(
  Date = c("2/1/2009", "3/1/2009", "4/1/2007", "6/1/2008", "7/1/2007", "8/1/2010"),
  Year = c(2009, 2009, 2007, 2008, 2007, 2010),
  Month = c(2, 3, 4, 6, 7, 8),
  Week = c(5, 9, 14, 22, 27, 31),
  Wday = rep("Sun", 6),
  Day = rep(1, 6),
  Time = rep(0, 6),
  ActiveEnergy = c(55.36111, 9.334444, 41.27667, 32.89833, 27.11667, 4.871111)
)

# UI部分
ui <- dashboardPage(
  dashboardHeader(title = "Energy Consumption Dashboard"),
  dashboardSidebar(
    sidebarMenu(
      # 「Historic」主菜单
      menuItem("Historic", tabName = "historic", icon = icon("history"))
    )
  ),
  dashboardBody(
    tabItems(
      # 「Historic」对应的页面内容
      tabItem(tabName = "historic",
              fluidRow(
                # 日期范围选择器
                box(width = 12,
                    dateRangeInput(
                      inputId = "date_range",
                      label = "Select Date Range:",
                      start = min(as.Date(df$Date, format = "%m/%d/%Y")),
                      end = max(as.Date(df$Date, format = "%m/%d/%Y")),
                      format = "mm/dd/yyyy"
                    )
                ),
                # 条形图输出区域
                box(width = 12,
                    plotOutput(outputId = "energy_barplot")
                )
              )
      )
    )
  )
)

# Server部分
server <- function(input, output) {
  # 动态处理数据:过滤日期范围 + 聚合计算
  filtered_energy_data <- reactive({
    df %>%
      # 把原始Date字符串转成标准日期格式
      mutate(Date = as.Date(Date, format = "%m/%d/%Y")) %>%
      # 根据用户选择的日期范围过滤数据
      filter(Date >= input$date_range[1] & Date <= input$date_range[2]) %>%
      # 按日期聚合,计算每日总ActiveEnergy(可替换为mean()求平均值)
      group_by(Date) %>%
      summarise(TotalActiveEnergy = sum(ActiveEnergy, na.rm = TRUE)) %>%
      ungroup()
  })
  
  # 渲染ggplot条形图
  output$energy_barplot <- renderPlot({
    ggplot(filtered_energy_data(), aes(x = Date, y = TotalActiveEnergy)) +
      geom_bar(stat = "identity", fill = "#2E86AB") +
      labs(
        title = "Active Energy Consumption Over Selected Period",
        x = "Date",
        y = "Total Active Energy"
      ) +
      theme_minimal() +
      # 旋转X轴日期标签,避免重叠
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
  })
}

# 运行Shiny应用
shinyApp(ui = ui, server = server)

关键细节说明

  • 日期格式转换:原始数据的Date列是字符串,必须转成Date类型才能让日期范围选择器正确过滤;
  • 数据聚合:这里用sum()计算每日的总能耗,如果你需要展示平均值,直接把sum改成mean即可;
  • 交互逻辑:用reactive()函数动态响应用户的日期选择,每次调整日期范围都会自动重新计算数据并更新图表;
  • 图表优化:添加了X轴标签旋转,避免日期文本重叠,同时用了简洁的theme_minimal()风格。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:09:39