Filtering a Dataframe based on Hours

Dear 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 here 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
})
}

Hi @Kostas87, if I understand well this is a option:

df <- data.frame(
  start_time = c("2022-04-01 00:00:00", "2022-05-01 01:00:00", "2022-04-01 04:41:00", 
                 "2022-04-01 06:00:00", "2022-06-01 17:01:00", "2022-07-01 18:00:00", 
                 "2022-08-01 21:00:00", "2022-09-02 00:00:00", "2022-10-02 01:00:00", 
                 "2022-11-02 02:41:00"))


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

server <- function(input, output) {
  output$filteredData <- renderDataTable({
    filtered_df <- df %>%
      filter(as.Date(start_time) >= input$dateRange[1] & 
               as.Date(start_time) <= input$dateRange[2])
    filtered_df
  })
}

shinyApp(ui = ui, server = server)

@M_AcostaCH Thank you for your response. While your answer brought me closer to my desired outcome, it hasn't quite reached it yet. Specifically, I am looking to utilize the dateRangeInput() widget to select data from a dataframe (df) between the dates and times of "2022-04-01 06:00:00" and "2022-04-02 01:00:00". I want to exclude any data before 06:00:00 on April 1, 2022, and any data after 01:00:00 on April 2, 2022. I hope my question is now clearer, and I would appreciate any suggestions you may have to help me achieve this?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.