I have some data that I am working on for a project that involves plotting data collected from the same location over multiple weeks. I am trying to get a map that only displays the markers that correspond to the date on the time slider, like the leaftime package allows for. However, when I run this code, the markers are all displayed at once, probably because I used the addMarker() in my leaflet map to get popups. So, I am mainly asking if there is a way to either hide markers that do not correspond to the current date on the time slider, or I would like for a way to include popup information on the leaftime markers that are included on the slider. Some example code below.
library(dplyr)
library(lubridate)
library(leaflet)
library(leaftime)
#creating the data frame
ex_df <- data.frame(location = c('park', 'shopping center', 'school', 'library'),
latitude = c(50.432543,50.438524,50.445140,50.399818),
longitude = c(-78.534095, -78.567779,-78.523978,-78.507879),
count_1 = c(234, 3565, 4, 95),
count_2 = c(3, 6759, 280, 4820),
start = c('7/24/1999', '7/31/1999', '8/7/1999','8/19/1999'),
end = c('7/25/1999','8/1/1999','8/8/1999','8/20/1999'),
detected = c('detected', 'detected', 'not detected', 'detected'))
#change some of the values to dates and add the week of collection
ex_df<-ex_df %>%
mutate(start = mdy(start)) %>%
mutate(end = mdy(end)) %>%
mutate(week = epiweek(start))
#add the popup
ex_df<-ex_df %>%
mutate(popup_tab = paste0("<b>location:<b>",
location, "<br>",
"<b>count_1:<b>",
count_1, "<br>",
"<b>count_2: <b>",
count_2, "<br>",
"<b>detected: <b>",
detected, "<br>"))
#change to a geojson object because thats what leaftime wants
ex_geo <- geojsonio::geojson_json(ex_df, lat = "latitude", lon = "longitude")
#create the leaflet map .
leaflet(ex_geo) %>%
addTiles() %>%
addMarkers(data = ex_df,
lng = ~longitude, lat = ~latitude,
label = ~paste0("Week:", week),
popup = ~popup_tab,
clusterOptions = markerClusterOptions()
) %>%
#creates the time slider
addTimeline(
sliderOpts = sliderOptions(
formatOutput = htmlwidgets::JS(
"function(date) {return new Date(date).toDateString()}"
),
position = "topright",
step = 4,
duration = 3000,
showTicks = TRUE))