Biogen-Inc / IDEAFilter

An R Shiny Filtering module. Demo the shiny module at: https://bit.ly/demo_IDEAFilter
https://biogen-inc.github.io/IDEAFilter/
Other
11 stars 2 forks source link

Incorrect handling of datetime values #17

Open Jeff-Thompson12 opened 11 months ago

Jeff-Thompson12 commented 11 months ago

https://github.com/Biogen-Inc/IDEAFilter/blob/66934c0399ddc6030117cdddbd64df486760c18d/R/shiny_vector_filter_datetime.R#L20-L22

The filter is already using S3 classes to determine which shiny_vector_filter() function to use, but then the function itself tries to change the data type. This leads to inaccurate filtering for the time. Possible solution might be to extract the tzone attribute from the vector. You will notice in the GIF below that the filter only engages once the hour is clicked to 17 instead of 12 because of the difference in timezone. IDEAFilter_datetime

Jeff-Thompson12 commented 11 months ago

I suppose I should also provide a reprex

data_frame = data.frame(col1 = letters[1:4],
                        col2 = c(5:8) ,
                        col3 = c("2021-05-05 01:04:34",
                                 "2021-03-06 03:14:44",
                                 "2021-03-11 07:22:48",
                                 "2021-02-02 11:54:56"))

data_frame[['col3']] <- as.POSIXct(data_frame[['col3']],
                                   format = "%Y-%m-%d %H:%M:%S")

ui <- fluidPage(
  titlePanel("Filter Data Example"),
  fluidRow(
    column(8,
      verbatimTextOutput("data_summary"),
      verbatimTextOutput("data_filter_code")),
    column(4, IDEAFilter::shiny_data_filter_ui("data_filter"))))

srv <- function(input, output, session) {
  filtered_data <- callModule(
    IDEAFilter::shiny_data_filter,
    "data_filter",
    data = data_frame,
    verbose = FALSE)

  output$data_filter_code <- renderPrint({
    cat(gsub("%>%", "%>% \n ",
      gsub("\\s{2,}", " ",
        paste0(
          capture.output(attr(filtered_data(), "code")),
          collapse = " "))
    ))
  })

  output$data_summary <- renderPrint({
    if (nrow(filtered_data())) show(filtered_data())
    else "No data available"
  })
}

shinyApp(ui, srv)