0

I have a Leaflet map in R Shiny with circle markers which change colour based on a time series slider. When a year is changed, the previously drawn circle marker is erased, and a new one is drawn. I want to know if there's a way to 'animate' the erasure/creation of addCircleMarkers() so that the transitions when a year is changed are not so abrupt.

Reproducible example below:

library(shiny)
library(sf)
library(leaflet)
library(tidyverse)
library(RColorBrewer)

set.seed(101)

dat <- data.frame(
  lat = rep(-27.4689682, 5),
  lng = rep(153.0234991, 5),
  val = rnorm(5, mean = 20, sd = 5),
  year = seq(2010, 2014)
)

ui <- fluidPage(
  sliderInput(
    inputId = "year_slider",
    label = "",
    min = 2010,
    max = 2014,
    value = 2010,
    animate = TRUE,
    sep = ""
  ),
  
  
  leafletOutput("map_output")
)


server <- function(input, output) {
  

  Filtered_dat <- reactive({
    dat %>%
      filter(year == input$year_slider)
  })
  
  output$map_output <- renderLeaflet({
    leaflet() %>%
      setView(lat = -27.4689682, lng = 153.0234991, zoom = 9) %>%
      addProviderTiles(providers$Esri.WorldGrayCanvas)
  })
  
  observe({
    pal <- colorNumeric(palette = "Greens", domain = dat$val)
    glimpse(pal(Filtered_dat()$val))
    
    leafletProxy("map_output") %>%
      clearMarkers() %>%
      addCircleMarkers(
        data = Filtered_dat(),
        lat = ~lat,
        lng = ~lng,
        stroke = FALSE,
        radius = 30,
        fillOpacity = 0.8,
        fillColor = ~pal(Filtered_dat()$val)
      )
  })
  
}


shinyApp(ui = ui, server = server)

1

0

Browse other questions tagged or ask your own question.