闪亮/ DT:列& datetime列的顶部过滤器显示错误的值 [英] Shiny/DT: column & top filter for datetime column show wrong values

查看:62
本文介绍了闪亮/ DT:列& datetime列的顶部过滤器显示错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚为什么我的列和过滤器的datetime列显示错误的日期和时间。

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

我的数据看起来像这样( dput 在下面的闪亮代码中):

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

我们可以看到,没什么特别的。使用 DT 包将数据显示为闪亮后,数据如下所示:

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

显示2h差异,没有任何原因...

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

我的第一种方法是检查我的 Sys.time()

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

这是正确的,第二种方法是要深入研究 DT 文档,在这里我发现了以下功能: formatDate(1,method ='toLocaleString')。我用过它,datetime字段的显示看起来不错(请参见下图),但是顶部过滤器仍然显示错误的datetime值...

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...

以下是可重现的示例:

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)

数据 min 日期时间值是 2017-03-29 00:00:02 ,但是<$的顶部过滤器c $ c> datatable 显示 2017-03-28T22:00:02 ,而 max 数据中的日期时间值为: 2017-03-29 00:01:07 ,顶部过滤器显示: 2017-03-28T22:01 :07

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.

我会感谢您的帮助和解释,为什么 DT 包会转换我的数据并以2h的差显示它,以及为什么在暗示函数 formatDate()之后,过滤器仍显示错误的值。

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.

感谢帮助

推荐答案

noUISlider的代码为此处。似乎所有日期都被当作UTC来获取滑块的最大值和最小值。

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.

您可以将日期和时间转换为相同的日期和时间在UTC时区中,以使滑块和日期之间保持一致:

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)

移动滑块时,R控制台中会出现警告指出时区不一致,但过滤有效。

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

这篇关于闪亮/ DT:列&amp; datetime列的顶部过滤器显示错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆