I'm working a dashboard in R using Shiny and leaflet. It is basically a map that changes when you select a date. What I want to do is to keep the "groups" selected in the same map position after changing the date.
This is a simplified version of my code:
library(leaflet)
library(tidyverse)
library(shiny)
data <- readRDS("data.RDS")
pal_01 <- colorNumeric("YlGnBu", domain = log1p(data$px100m))
pal_02 <- colorNumeric("YlOrBr", domain = log1p(data$fx100m))
ui <- fluidPage(
titlePanel("TITLE"),
sidebarLayout(
sidebarPanel(
selectInput("PERIODO_ID", "Select PERIODO_ID:",
choices=sort(unique(data$PERIODO_ID), decreasing=TRUE),
),
),
mainPanel(tabPanel("MAP", leafletOutput("MAP")))
)
)
server <- function(input, output, session) {
data_f <- reactive({
x <- data %>% filter(PERIODO_ID==input$PERIODO_ID)
return(x)
})
output$MAP <- renderLeaflet({
leaflet() %>%
addTiles(group="OSM") %>%
addProviderTiles("CartoDB", group="CartoDB") %>%
addProviderTiles("Esri", group="Esri") %>%
addPolygons(data=data_f(), weight = 1, color = ~pal_01(log1p(px100m)),group="A") %>%
addPolygons(data=data_f(), weight = 1, color = ~pal_02(log1p(fx100m)),group="B") %>%
addLayersControl(baseGroups = c("CartoDB","OSM","Esri"),
overlayGroups = c("A","B")) %>%
hideGroup("A") %>%
hideGroup("B")
})
}
shinyApp(ui = ui, server = server)
As you can see, I'm filtering my dataset using PERIODO_ID. I'm using 3 Tiles (OSM, CartoDB, Esri) and plotting 2 layers of polygons (A,B) I want to start my map with no polygons selected, mantaining both baseGroups and overlayGroups according to the user's selection after changing the date. If I could additionally mantain the map position would be awesome.
From what I have read, I tried using leafletProxy and observeEvent but still doesn't work.