[R] Filtering DataFrame by Hours using dateRangeInput in Shiny

konstantinos christodoulou kon@t@nt|no@@chr|@todou|ou1 @end|ng |rom gm@||@com
Wed May 24 09:47:54 CEST 2023


Dear R Support Team,

I have a dataframe in R with a column named 'start_time,' which is
classified as POSIXct. My objective is to use the dateRangeInput() widget
in Shiny to filter the dataset based on the 'start_time' column,
specifically by selecting hours on a particular day.

My dataframe is given below:

*df* <- data.frame(*start_time* = c(2022-04-01 00:00:00, 2022-04-01
01:00:00, 2022-04-01 04:41:00, 2022-04-01 06:00:00, 2022-04-01
17:01:00, 2022-04-01
18:00:00, 2022-04-01 21:00:00, 2022-04-02 00:00:00, 2022-04-02
01:00:00, 2022-04-02
02:41:00)

However, when I implement this approach,I receive the results grouped by
days (Instead of obtaining the filtered dataframe based on hours, e.g
2022-04-01
04:41:00 until 2022-04-02 01:00:00). I would greatly appreciate your
assistance in resolving this matter.

I also give the Rshinny code:

ui <- fluidPage(

titlePanel("Filtering Data by Hour"), sidebarLayout( sidebarPanel(
dateRangeInput("dateRange", "Select Date Range:", start = min(df$start_time)
, end = max(df$start_time), min = min(df$start_time), max = max(df$
start_time) ), mainPanel( dataTableOutput("filteredData") ) ) )

server <- function(input, output) { output$filteredData <- renderDataTable({

filtered_df <- df %>%

filter(start_time >= input$dateRange[1] & start_time <= input$dateRange[2])

filtered_df }) }

Thank you very much!

	[[alternative HTML version deleted]]



More information about the R-help mailing list