1

I have a leaflet map with a selection of base groups and overlay groups, when i use the code

input$mymap_group[1]

this only shows me the first of the overlay groups selected not the base group. Is there a way to show the current selected base group?

library(shiny)
library(leaflet)

evar <- c("SST", "SLA", "CHL", "Eddies NS", "Eddies EW", "NPP")
groups <- c("12323", "1232455","3443", "23","987", "566")

ui <- fluidPage(
  mainPanel(tags$style(type = "text/css", "#mymap {height: calc(100vh - 80px) !important;}"),
            uiOutput("group_selected"),
            leafletOutput("mymap"))
)

server <- function(input, output, session) {

  output$group_selected <- renderUI({ h2(input$mymap_groups[1]) })

    output$mymap <- renderLeaflet({
      leaflet() %>% addTiles() %>% setView(lng = -20, lat = 14, zoom = 6) %>% addLayersControl(baseGroups = evar, overlayGroups = groups)
    })

}

runApp(shinyApp(ui, server))
Tom Ivall
  • 33
  • 6
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Axeman Jul 20 '17 at 11:59
  • I have, now are you able to help? – Tom Ivall Jul 20 '17 at 12:13

2 Answers2

1

I am not 100% sure this will solve your issue, but it might point you in the right direction. If there is always a one to one relation between group and overlaygroup, this might work:

library(shiny)
library(leaflet)

evar <- c("SST", "SLA", "CHL", "Eddies NS", "Eddies EW", "NPP")
groups <- c("12323", "1232455","3443", "23","987", "566")

ui <- fluidPage(
  mainPanel(tags$style(type = "text/css", "#mymap {height: calc(100vh - 80px) !important;}"),
            textOutput("group_selected"),
            leafletOutput("mymap"))
)

server <- function(input, output, session) {

  output$group_selected <- renderText({ evar[which(groups %in% input$mymap_groups )] })

  output$mymap <- renderLeaflet({
    leaflet() %>% addTiles() %>% setView(lng = -20, lat = 14, zoom = 6) %>% addLayersControl(baseGroups = evar, overlayGroups = groups)
  })

}

runApp(shinyApp(ui, server))

You can use leafletProxy to select groups. https://rstudio.github.io/leaflet/shiny.html. Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80
  • Sorry, it didn't actually fully solve it. Your code gives me a list of groups that I already have. Is there a way I can get all those selected in input$mymap_groups? – Tom Ivall Jul 20 '17 at 13:49
  • What do you expect to be in input$mymap_groups? In the sample code you gave, it is never updated right? – Florian Jul 20 '17 at 13:50
1

This question is a few years old but as far as I know, there's still no way to implement this without adding a bit of javascript to your R code. Here is some code to create a new input binding with the name of the selected base group (or tile layer, background, whatever you want to call it):

output$my_map <- renderLeaflet({
  leaflet() %>% 
    addLayersControl(baseGroups = c("light", "satellite")) %>% 
    addProviderTiles(providers$CartoDB.Positron, group = "normal") %>%
    addProviderTiles(providers$Esri.WorldImagery, group = "satellite") %>%
    htmlwidgets::onRender("
      function(el, x) {
        var myMap = this;
        myMap.on('baselayerchange',
          function (e) {
            Shiny.onInputChange('my_map_tile', e.layer.groupname)
        })
    }")
})

Your base group selection then appears in input$my_map_tile

observeEvent(input$my_map_tile, {
  # Do something...
})
Dharman
  • 30,962
  • 25
  • 85
  • 135
comaes
  • 36
  • 5
  • I'm having an issue with the `baseGroups` rendering when using leafletProxy. Let's say I have 2 base groups, where the first one is showing on the map. When `leafletProxy` is called for `addLayersControl` with only the 2nd base group, the `LayersControl` is rendered fine, but does not select (fill in the circle) of the remaining `baseGroup`. Make sense? How to select the default `baseGroup` so that is is selected (filled in) in the UI ? Thanks! – kraggle May 25 '22 at 20:12
  • I figured it out, `leaflet::showGroup()` obviously... – kraggle May 25 '22 at 20:30