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

RShiny披萨预订工具:动态更新可用取餐时段SelectInput

披萨预订工具时段过滤实现方案

核心逻辑

要实现动态过滤可用取餐时段,需完成以下步骤:

  • 预定义所有15分钟间隔的取餐时段
  • 监听用户选择的预订日期和披萨数量变化
  • 聚合历史数据,计算所选日期各时段的已预订披萨总数
  • 筛选出满足「已预订数 + 当前用户预订数 < 7」的时段,实时更新选择框

分文件代码实现

1. Global.R(全局配置与数据)

# 预定义15分钟间隔的取餐时段(可根据实际营业时间调整)
all_time_slots <- seq(
  from = as.POSIXct("2024-01-01 10:00:00"),
  to = as.POSIXct("2024-01-01 20:00:00"),
  by = "15 mins"
) %>% format("%H:%M")

# 读取本地历史预订数据(替换为你的实际存储路径,比如CSV/数据库)
# 示例:若用CSV存储,替换为 historical_bookings <- read.csv("bookings.csv")
historical_bookings <- data.frame(
  date = as.Date(c("2024-07-15", "2024-07-15", "2024-07-16")),
  time_slot = c("11:00", "11:15", "12:00"),
  pizza_count = c(3, 5, 6)
)

2. UI.R(界面部分)

library(shiny)

ui <- fluidPage(
  titlePanel("夏季活动披萨预订"),
  sidebarLayout(
    sidebarPanel(
      # 个人信息表单(保留你已有的代码)
      textInput("name", "姓名"),
      textInput("phone", "联系电话"),
      
      # 预订日期选择
      dateInput("booking_date", "预订日期", min = Sys.Date()),
      
      # 披萨数量滑块
      sliderInput("pizza_num", "披萨数量", min = 1, max = 6, value = 1),
      
      # 动态生成的可用时段选择框(替换原固定selectInput)
      uiOutput("available_time_slots")
    ),
    mainPanel(
      actionButton("submit", "提交预订")
    )
  )
)

3. Server.R(逻辑处理)

library(shiny)
library(dplyr)

server <- function(input, output, session) {
  # 实时计算所选日期各时段的已预订总数
  booked_counts <- reactive({
    # 确保预订日期已选择
    req(input$booking_date)
    
    historical_bookings %>%
      filter(date == input$booking_date) %>%
      group_by(time_slot) %>%
      summarise(total_booked = sum(pizza_count), .groups = "drop")
  })
  
  # 动态生成可用时段选择框
  output$available_time_slots <- renderUI({
    # 确保披萨数量和已预订数据已就绪
    req(input$pizza_num, booked_counts())
    
    # 合并所有时段与已预订数据,未被预订的时段总数设为0
    all_slots_with_counts <- data.frame(time_slot = all_time_slots) %>%
      left_join(booked_counts(), by = "time_slot") %>%
      mutate(total_booked = ifelse(is.na(total_booked), 0, total_booked))
    
    # 筛选可用时段:已预订数 + 当前预订数 < 7
    available_slots <- all_slots_with_counts %>%
      filter(total_booked + input$pizza_num < 7) %>%
      pull(time_slot)
    
    # 生成选择框,无可用时段时显示空选项
    selectInput(
      "time_slot", "取餐时段",
      choices = available_slots,
      selected = if(length(available_slots) > 0) available_slots[1] else ""
    )
  })
  
  # 预订提交逻辑(保留你已有的本地存储代码,以下为示例)
  observeEvent(input$submit, {
    new_booking <- data.frame(
      date = input$booking_date,
      time_slot = input$time_slot,
      pizza_count = input$pizza_num,
      name = input$name,
      phone = input$phone,
      timestamp = Sys.time()
    )
    
    # 追加到本地CSV文件
    write.table(
      new_booking,
      "bookings.csv",
      append = TRUE,
      sep = ",",
      row.names = FALSE,
      col.names = !file.exists("bookings.csv")
    )
    
    showModal(modalDialog("预订成功!"))
  })
}

shinyApp(ui, server)

关键说明

  • req()函数:确保依赖的输入(如预订日期、披萨数量)已存在,避免空值报错
  • left_join():处理未被预订的时段,确保所有预设时段都被纳入计算
  • 动态UI:用uiOutput和renderUI实现选择框的实时更新,替换原固定的selectInput
  • 数据持久化:提交逻辑中,write.table的append=TRUE保证新预订追加到历史数据中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 17:40:58