Well, it's not the most beautiful code ever written, but it behaves as I'd like. As you mentioned, cumulative addition of layers eliminates the flashing, but also taxes the client quite a bit. So I amended the script to clear the 3rd to last layer added, unless the user drags the slider to a previous year, or the loop recycles, in which case all layers are cleared (still flashes, but really the forward temporal continuity is what I'm after).
library(shiny)
library(leaflet)
ui <- fluidPage(
fluidRow(
column(4, offset = 4,
sliderInput(
"sliderIn",
"Año",
2001,
2016,
value = 2001,
step = 1,
sep = "",
width = 600,
animate =
animationOptions(interval = 1250, loop = T)
))
),
leafletOutput("map", width = "100%", height = "600px")
)
server <- function(input, output, session){
year <- reactive({
which(2001:2016 == input$sliderIn)
})
yearList <- reactiveValues(val = 0)
observeEvent(input$sliderIn,{
yearList$val <- c(yearList$val, year())
})
output$map <- renderLeaflet({
leaflet(options = leafletOptions(minZoom = 0, maxZoom = 16, zoomControl = F)) %>%
addTiles(urlTemplate = "https://mts1.google.com/vt/lyrs=s&hl=en&src=app&x={x}&y={y}&z={z}&s=G", attribution = 'Google') %>%
setView(lng = -84.8,
lat = 15.75,
zoom = 10) %>%
addTiles(urlTemplate = "https://storage.googleapis.com/honduras/fl/year1/{z}/{x}/{y}.png",
layerId = '1', group = 'ForestLoss',
options = tileOptions(minZoom = 9, maxZoom = 18, tms = TRUE))
})
observe({
if(year() > tail(yearList$val, 2)[1]){
leafletProxy(mapId = 'map') %>%
removeTiles(layerId = paste0(tail(yearList$val,3)[1])) %>%
addTiles(urlTemplate = paste0('https://storage.googleapis.com/honduras/fl/year',year() ,'/{z}/{x}/{y}.png'),
layerId = paste0(year()),
group = 'ForestLoss',
options = tileOptions(minZoom = 9, maxZoom = 18, tms = TRUE))
}else{
leafletProxy(mapId = 'map') %>%
clearGroup('ForestLoss') %>%
addTiles(urlTemplate = paste0('https://storage.googleapis.com/honduras/fl/year',year() ,'/{z}/{x}/{y}.png'),
layerId = paste0(year()),
group = 'ForestLoss',
options = tileOptions(minZoom = 9, maxZoom = 18, tms = TRUE))}
})
}
shinyApp(ui, server)