I am trying to create a time line plot using leaflet and leaftime packages. I want to set custom date in sliderOptions, such that I have both of Gregorian calendar and Persian calendar(or even pass any information from dataset). In here I saw following code to customize date:
sliderOpts = sliderOptions(
formatOutput = htmlwidgets::JS(
"function(date) {return new Date(date).toDateString()}
")
According to the above code, I try to pass data as an argument to the function, 'function(date, data)', and try to use the dataset variable by 'data.properties.persian_date' as follows:
library(leaflet)
library(leaftime)
library(htmltools)
power <- data.frame(
"Latitude" = c(
33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167,
54.140586, 54.140586, 48.494444, 48.494444
),
"Longitude" = c(
129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889,
13.664422, 13.664422, 17.681944, 17.681944
),
"start" = seq.Date(as.Date("2015-01-01"), by = "day", length.out = 10),
"end" = seq.Date(as.Date("2015-01-01"), by = "day", length.out = 10) + 1,
color_temp=rep(c("red","blue","green"),len=10), persian_date=c("1393/10/11","1393/10/12","1393/10/13","1393/10/14","1393/10/15","1393/10/16","1393/10/17","1393/10/18","1393/10/19","1393/10/20")
)
power_geo <- geojsonio::geojson_json(power,lat="Latitude",lon="Longitude")
leaflet(power_geo) %>%
addTiles() %>%
setView(44.0665,23.74667,2) %>%
addTimeline(
sliderOpts = sliderOptions(
formatOutput = htmlwidgets::JS(
"function(date,data) {return new Date(date).toDateString()+'<br/>' +'new
info'+data.properties.persian_date}
"),
position = "bottomright",
#step = 10,
duration = 6000,
showTicks = FALSE
)
,
timelineOpts = timelineOptions(
styleOptions = NULL,
pointToLayer = htmlwidgets::JS(
"
function(data, latlng) {
return L.circleMarker(
latlng,
{
radius: 25,
color: data.properties.color_temp,
fillColor: data.properties.color_temp,
fillOpacity: 1
}
).bindPopup( data.properties.color_temp+ ' <br/> '+ data.properties.persian_date+ ' <br/> '+ data.properties.start);
}
"
)
)
)
But unfortunately it does not work. If I remove '+data.properties.persian_date' part, the code run fine:
leaflet(power_geo) %>%
addTiles() %>%
setView(44.0665,23.74667,2) %>%
addTimeline(
sliderOpts = sliderOptions(
formatOutput = htmlwidgets::JS(
"function(date,data) {return new Date(date).toDateString()+'<br/>' +'new data' }
"),
position = "bottomright",
#step = 10,
duration = 6000,
showTicks = FALSE
)
,
timelineOpts = timelineOptions(
styleOptions = NULL,
pointToLayer = htmlwidgets::JS(
"
function(data, latlng) {
return L.circleMarker(
latlng,
{
radius: 25,
color: data.properties.color_temp,
fillColor: data.properties.color_temp,
fillOpacity: 1
}
).bindPopup( data.properties.color_temp+ ' <br/> '+ data.properties.persian_date+ ' <br/> '+ data.properties.start);
}
"
)
)
)
The error happen in '+data.properties.persian_date'. How to fix this?
' + pdt + '
' + pdtl; }")` – user20650 Jul 04 '21 at 12:09