首页 > 解决方案 > 在 R Shiny 中从特定工作日的密度中选择时间间隔

问题描述

我有 5 天的标记数据,我创建了一个闪亮的应用程序,用户可以在其中选择显示哪一天:根据绘制的标签有数据密度。我想通过在范围滑块上选择范围来添加选项以从所选日期选择时间间隔,并且我希望标签计数作为所选间隔的输出。我也用刷子选项试过这个,但它没有用。

ui = fluidPage(
selectInput("day","Choose a day:",choices = unique(weekdays(data$Timestamp))),
plotOutput("myplot", brush = brushOpts(id ="plot_brush", direction = "x")),
verbatimTextOutput("info"))

server <- function(input, output, session){

output$myplot <- renderPlot({
selected_subset  = data[which(weekdays(data$Timestamp)==input$day),]
labels =     as.factor(data[which(weekdays(data$Timestamp)==input$day),]$Label)

gg <- ggplot(selected_subset,aes(x = Timestamp, fill= labels)) + 
    stat_density(alpha=.75, show.legend = T, trim= T)  + 
    scale_x_datetime(breaks = date_breaks("1 hour"), labels=date_format("%H:%M"))

plot(gg)
})

D=reactive({brushedPoints(data[which(weekdays(data$Timestamp)==input$day),],brush = input$plot_brush, xvar=Timestamp)
})

output$info <- renderPrint({D()})   
}

shinyApp(ui, server)

标签: timeshiny

解决方案


我实际上已经解决了这个问题,只需在 ui 中向 sliderInput 添加参数。这是代码:

ui <- fluidPage(
   selectInput("day","Choose a day:",choices = unique(weekdays(data$Timestamp)),selected = "Tuesday"), 
   sliderInput("time_range", label = "Time range:",
          min = min(data[which(weekdays(data$Timestamp)=="Tuesday"),]$Timestamp), 
          max = max(data[which(weekdays(data$Timestamp)=="Tuesday"),]$Timestamp), 
          value = c(min(data[which(weekdays(data$Timestamp)=="Tuesday"),]$Timestamp), 
                    max(data[which(weekdays(data$Timestamp)=="Tuesday"),]$Timestamp)),
          step = 10,timezone = "UTC",timeFormat="%H:%M:%S", dragRange = TRUE),
                    # dragRange means that min and max can be dragged together
                    # timezone must be the same as is in the beginning of document


   plotOutput("myplot"),
   verbatimTextOutput("info"))

server <- function(input, output, session) {
    observe({   # changing of slider input for time on selected day
    updateSliderInput(session, "time_range", 
                  min = min(data[which(weekdays(data$Timestamp)==input$day),]$Timestamp), 
                  max = max(data[which(weekdays(data$Timestamp)==input$day),]$Timestamp), 
                  value = c(min(data[which(weekdays(data$Timestamp)==input$day),]$Timestamp), 
                            max(data[which(weekdays(data$Timestamp)==input$day),]$Timestamp)),
                  step = 10,timezone = "UTC",timeFormat="%H:%M:%S")})

     output$myplot <- renderPlot({
          selected_subset=data[which(weekdays(data$Timestamp)==input$day),]  
          labels = as.factor(data[which(weekdays(data$Timestamp)==input$day),]$Label)

       gg <- ggplot(selected_subset,aes(x = Timestamp, fill= labels)) + 
    stat_density(alpha=.75, show.legend = T, kernel = "rectangular", trim= T)  + 
    scale_x_datetime(breaks = date_breaks("1 hour"), labels=date_format("%H:%M")) +
    coord_cartesian(xlim = c(input$time_range[1],input$time_range[2]))

    plot(gg)
})

     output$info <- renderPrint({
selected_timestamp=data[which(weekdays(data$Timestamp)==input$day),]$Timestamp
print(table(data[which((selected_timestamp<=input$time_range[2])&(selected_timestamp>=input$time_range[1])),]$Label))
  })

 }

shinyApp(ui, server)

推荐阅读