-->

Shiny/DT: column & top filter for datetime column

2019-05-24 22:48发布

问题:

I am having bit of trouble to figure out why my column and filter for datetime column shows the wrong date and time.

My data looks like this (dput is in shiny code below):

                 DATUM NUMMER
1  2017-03-29 00:00:02     19
2  2017-03-29 00:00:36     20
3  2017-03-29 00:00:40     21
4  2017-03-29 00:00:44     22
5  2017-03-29 00:00:47     23
6  2017-03-29 00:00:51     24
7  2017-03-29 00:00:55     25
8  2017-03-29 00:00:59     26
9  2017-03-29 00:01:03     27
10 2017-03-29 00:01:07     28

As we can see, it is nothing special. After displaying this data in shiny using DT package, the data looks like this:

it is displayed with 2h difference, without any reason...

My first approach was to check my Sys.time()

> Sys.time()
[1] "2017-03-30 09:09:40 CEST"

and it is correct, second approach was to dig into DT documentation, and there i have found the function: formatDate(1, method = 'toLocaleString'). I have used it, the display of the datetime field appeared to be good (see pic below), however the top filter still is showing the wrong datetime values...

Here is the reproducible example:

library(shiny)
library(DT)

data <- structure(list(DATUM = structure(c(1490738402, 1490738436, 1490738440, 
                                           1490738444, 1490738447, 1490738451, 1490738455, 1490738459, 1490738463, 
                                           1490738467), class = c("POSIXct", "POSIXt"), tzone = "CEST"), NUMMER = c(19, 
                                                                                                                20, 21, 22, 23, 24, 25, 26, 27, 28)), .Names = c("DATUM", "NUMMER"
                                                                                                                ), row.names = c(NA, 10L), class = "data.frame")

ui= fluidPage(

      dataTableOutput("tab")
    )

server= function(input, output,session) {

  output$tab <- DT::renderDataTable({
    datatable(data,rownames=TRUE, filter="top", class = 'cell-border stripe') %>%
      formatDate(1, method = 'toLocaleString')})


}

shinyApp(ui, server)

As we can see in the data the min datetime value is 2017-03-29 00:00:02, however the top filter of datatable shows 2017-03-28T22:00:02, whereas the max datetime value in data is: 2017-03-29 00:01:07, the top filter shows: 2017-03-28T22:01:07.

I would appreciate any help and explanation, why DT package transforms my data and displays it with 2h difference, and why after implying the function formatDate(), the filter still shows wrong values.

Thanks for help

回答1:

The code for the noUISlider is here. It looks like all the dates are treated as UTC when to get the min and max for the slider.

You can maybe transform the dates and times to the same dates and time in the UTC timezone to have consistency between the slider and your dates:

data$DATUM <- as.POSIXct(as.character(data$DATUM), tz="UTC")

ui=fluidPage(

  dataTableOutput("tab")
)

server= function(input, output,session) {

  output$tab <- DT::renderDataTable({
    datatable(data,rownames=TRUE, filter="top", class = 'cell-border stripe') %>%
      formatDate(1, method = 'toISOString')})


}

shinyApp(ui, server)

When the slider is moved, there is a warning in the R console stating that time zones are inconsistent but the filtering works.