Layer control in timeline map created using leaftime plug-in for leaflet in R

Viewed 226

Is it possible to add layer control to a map with a timeline? (created using R leaftime package: https://github.com/timelyportfolio/leaftime).

In the below example, I want to group all the points I have as 'Food' and have it disappear / re-appear when I click on the layer control option that appears on the top-right hand corner. But when I run the code in R, I get a pop up on the top right that I can click on with 'Food' as a category but clicking it doesn't make the points from not appearing when I start the timer.

I had thought adding addTimeline( group = 'Food'....) would have made it behave how I wanted it to but it doesn't seem to and I'm not sure how I can get it to appear / disappear by clicking the layer control button.

if(interactive()) {

  library(leaflet)
  library(leaftime)
  library(htmltools)

  #Build data.frame with 10 obs + 3 cols
  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
  )

  # use geojsonio to convert our data.frame
  #  to GeoJSON which timeline expects
  power_geo <- geojsonio::geojson_json(power,lat="Latitude",lon="Longitude")



# to style each point differently based on the data
power_styled <- power
# IE does not like alpha so strip colors of alpha hex
power_styled$color <- substr(topo.colors(6)[ceiling(runif(nrow(power),0,6))],1,7)
power_styled$radius <- seq_len(nrow(power_styled)) # ceiling(runif(nrow(power),3,10))

leaflet(geojsonio::geojson_json(power_styled)) %>%
  addTiles() %>%
  setView(44.0665,23.74667,2) %>%
  # addCircleMarkers(
  #   data = power_styled, lat = ~Latitude, lng = ~Longitude, radius = 11
  # ) %>%
  addTimeline(
    group = 'Food', 
    timelineOpts = timelineOptions(
      styleOptions = NULL, # make sure default style does not override
      pointToLayer = htmlwidgets::JS(
        "
function(data, latlng) {
  return L.circleMarker(
    latlng,
    {
      radius: +data.properties.radius,
      color: data.properties.color,
      fillColor: data.properties.color,
      fillOpacity: 1
    }
  );
}
"
      )
    )
  ) %>% 
  addLayersControl(overlayGroups = 'Food')

}
0 Answers
Related