我如何解释从 ggvis handle_click 返回的日期
How can I interpret dates returned from ggvis handle_click
初体验handle_click
library(ggvis)
library(dplyr)
# create data.frame
counts <- c(1,2)
dates <- as.Date(c("2015-01-01","2015-01-02"))
df <- data.frame(count=counts,date=dates)
# function to view clicked data
getDate = function(data,location,session){
if(is.null(data)) return(NULL)
glimpse(data)
}
df %>%
ggvis(~date,~count) %>%
layer_points() %>%
handle_click(getDate)
点击第一个点时returns
Observations: 1
Variables:
$ date (dbl) 1.42007e+12
$ count (int) 1
TIA
日期显示为距原始时间的毫秒数,原始时间被认为是 1970-01-01 00:00:00
。
如您所见,您可以使用以下方法将其转换回原始日期:
> as.POSIXct(1.42007e+12/1000, origin='1970-01-01 00:00:00 GMT')
[1] "2014-12-31 23:53:20 GMT"
6分钟的误差我想是由于舍入误差造成的。
您可以更改脚本以查看日期,因为它们应该如下所示:
# function to view clicked data
getDate = function(data,location,session){
if(is.null(data)) return(NULL)
#just added the below line in your code
data$date <- as.Date(as.POSIXct((data$date)/1000, origin='1970-01-01 00:10:00 GMT'))
glimpse(data)
}
df %>%
ggvis(~date,~count) %>%
layer_points() %>%
handle_click(getDate)
returns 点击:
Observations: 1
Variables:
$ date (date) 2015-01-01
$ count (dbl) 1
初体验handle_click
library(ggvis)
library(dplyr)
# create data.frame
counts <- c(1,2)
dates <- as.Date(c("2015-01-01","2015-01-02"))
df <- data.frame(count=counts,date=dates)
# function to view clicked data
getDate = function(data,location,session){
if(is.null(data)) return(NULL)
glimpse(data)
}
df %>%
ggvis(~date,~count) %>%
layer_points() %>%
handle_click(getDate)
点击第一个点时returns
Observations: 1
Variables:
$ date (dbl) 1.42007e+12
$ count (int) 1
TIA
日期显示为距原始时间的毫秒数,原始时间被认为是 1970-01-01 00:00:00
。
如您所见,您可以使用以下方法将其转换回原始日期:
> as.POSIXct(1.42007e+12/1000, origin='1970-01-01 00:00:00 GMT')
[1] "2014-12-31 23:53:20 GMT"
6分钟的误差我想是由于舍入误差造成的。
您可以更改脚本以查看日期,因为它们应该如下所示:
# function to view clicked data
getDate = function(data,location,session){
if(is.null(data)) return(NULL)
#just added the below line in your code
data$date <- as.Date(as.POSIXct((data$date)/1000, origin='1970-01-01 00:10:00 GMT'))
glimpse(data)
}
df %>%
ggvis(~date,~count) %>%
layer_points() %>%
handle_click(getDate)
returns 点击:
Observations: 1
Variables:
$ date (date) 2015-01-01
$ count (dbl) 1