从R Shiny中特定工作日的密度选择时间间隔

时间:2019-03-25 12:41:49

标签: time 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)

1 个答案:

答案 0 :(得分:0)

我实际上已经解决了这个问题,只需在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)