首页 > 解决方案 > R闪亮:滑块输入与小时,反应和绘图之间的链接

问题描述

我没有成功在带小时的滑块输入和反应式之间建立链接,以更改我的情节中的小时范围......小时列的类型在我的脚本中是 hms,有人可以帮助我吗?非常感谢 :slight_smile:

library(shiny)
library(ggplot2)
library(dplyr)
library(shinyTime)
library(lubridate) #variable temps

route_fond_degre <- c(1:10)
vitesse_fond_noeuds <- c(2:11)
heure <- c("01:05:30","01:05:31","01:05:32","01:05:33","01:05:34","01:05:35","01:05:36",
                    "01:05:37","01:05:38","01:05:39")
df_test <- data.frame(heure,  route_fond_degre, vitesse_fond_noeuds)


ui <- fluidPage(

             sliderInput(inputId = "periode",
                         label = "Période",
                         min = as.POSIXct("10:00:00",format = "%H:%M:%S"),
                         max = as.POSIXct("23:00:00",format = "%H:%M:%S"),
                         value = c(as.POSIXct("10:00:00",format = "%H:%M:%S"), as.POSIXct("23:59:59",format = "%H:%M:%S")),
                         timeFormat = "%H:%M:%S"),
             #le slider s'affiche bien mais pas de lien avec le plot

             plotOutput("graph1")
  )

server <- function(input, output) {

    date_filtre <- reactive({

      mini <- as.POSIXct(input$periode[1])
      maxi <- as.POSIXct(input$periode[2])

      #df_test <- df_test %>% filter(heure > hms("10:00:00")) # this works without sliderinput

      data_sel <- df_test %>% filter(heure > mini & heure < maxi)
      return(data_sel)
    })

    output$graph1 <- renderPlot({

      g <- ggplot(date_filtre(), aes_string(x="heure", y = "route_fond_degre"))+
        geom_point()
      g

    })


}

shinyApp(ui=ui, server=server) 

标签: rshiny

解决方案


df_test$heure也转向POSIXct打字。

df_test$heure <- as.POSIXct(df_test$heure, format = '%T')

推荐阅读