I have a fairly complex Shiny app with a leaflet map on it and many other controls. I have made a very simplified version of it below.
I have set the two groups (an addMarker and an addCircles group) initially to not plot by placing both layers in a hideGroup function. I then run the app and manually switch them on in the addLayersControl in the checkbox on the map. The radius of the addCircles group is controlled reactively by a sliderInput, however, this then runs the hideGroup functions again and they are not plotted, which I now do not want to happen. I want them to remain plotted.
How do I start off with hidden groups, show them by switching them on via the map layer control and then send a new value to the addCirlces radius without the groups then switching back to the hidegroup default please?
Do I tackle the problem with isolate, I haven’t had any success that way yet? Or is there an if_else conditional approach I should take to invoke a showGroup state instead? Thanks in advance.
library(shiny)
library(tidyverse)
library(leaflet)
library(sf)
ui <- fluidPage(
column(width = 4,
sliderInput("slt_kam_reach",
"KAM",
min = 0,
max = 50000,
step = 1000,
value = 5000
)
),
column(width = 8,
leafletOutput("lflt_map")
)
)
kam_data <- data.frame(
kam_code = c("abc123", "def456"),
lng = c(18.5, 18.7),
lat = c(-33.5, -33.7)
)
kam_data_sf <- st_as_sf(
kam_data,
coords = c("lng", "lat"),
crs = "+proj=longlat +datum=WGS84"
)
server <- function(input, output, session){
output$lflt_map <- renderLeaflet({
leaflet() %>%
addTiles(group = "OSM") %>%
addLayersControl(
overlayGroups = c("KAM",
"KAM_reach"),
options = layersControlOptions(
collapsed = F
)
) %>%
addMarkers(data = kam_data_sf,
group = "KAM") %>%
addCircles(data = kam_data_sf,
group = "KAM_reach",
radius = input$slt_kam_reach
) %>%
hideGroup("KAM") %>%
hideGroup("KAM_reach")
})
}
shinyApp(ui, server)
I see you posted an answer as I finished mine. Here it is anyway:
library(shiny)
library(tidyverse)
library(leaflet)
library(sf)
ui <- fluidPage(
column(width = 4,
sliderInput("slt_kam_reach",
"KAM",
min = 0,
max = 50000,
step = 1000,
value = 5000
)
),
column(width = 8,
leafletOutput("lflt_map")
)
)
kam_data <- data.frame(
kam_code = c("abc123", "def456"),
lng = c(18.5, 18.7),
lat = c(-33.5, -33.7)
)
kam_data_sf <- st_as_sf(
kam_data,
coords = c("lng", "lat"),
crs = "+proj=longlat +datum=WGS84"
)
server <- function(input, output, session){
output$lflt_map <- renderLeaflet({
leaflet() %>%
addTiles(group = "OSM") %>%
addLayersControl(
overlayGroups = c("KAM",
"KAM_reach"),
options = layersControlOptions(
collapsed = F
)
) %>%
hideGroup("KAM") %>%
hideGroup("KAM_reach") %>%
fitBounds(min(kam_data$lng),
min(kam_data$lat),
max(kam_data$lng),
max(kam_data$lat))
})
observeEvent(input$slt_kam_reach, {
leafletProxy("lflt_map") %>%
clearGroup("KAM_reach") %>%
addMarkers(data = kam_data_sf,
group = "KAM") %>%
addCircles(data = kam_data_sf,
group = "KAM_reach",
radius = input$slt_kam_reach
)
})
}
shinyApp(ui, server)
Update
You could use an observe as well which is probably neater:
observe({
leafletProxy("lflt_map") %>%
clearGroup("KAM_reach") %>%
addMarkers(data = kam_data_sf,
group = "KAM") %>%
addCircles(data = kam_data_sf,
group = "KAM_reach",
radius = input$slt_kam_reach
)
})