First experience of using 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)
This returns when clicking the first point
Observations: 1
Variables:
$ date (dbl) 1.42007e+12
$ count (int) 1
TIA
The dates appear as milliseconds from the origin time which is considered to be 1970-01-01 00:00:00
.
As you can see you can transform that back to your original date using:
> as.POSIXct(1.42007e+12/1000, origin='1970-01-01 00:00:00 GMT')
[1] "2014-12-31 23:53:20 GMT"
The error of 6minutes is due to a rounding error I suppose.
You could change your script to see the dates as they should appear like this:
# 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)
Which returns on click:
Observations: 1
Variables:
$ date (date) 2015-01-01
$ count (dbl) 1