1

How can the legend be 'turned off' dynamically with googleway? Here is the code adapted from the googleway vignette (this example is updated from a previous version based on a slider and is hopefully more relevant to this question)

library(googleway)
library(tidyverse)
library(shiny)

ui <- fluidPage(
  checkboxInput("check", "Fill polygons"),
  google_mapOutput(outputId = "map")
)

server <- function(input, output){

  output$map <- renderGoogle_map({
    google_map(key = "") %>%
      add_polygons(data = melbourne, id = "polygonId", pathId = "pathId", 
                   polyline = "polyline", fill_opacity = 0, fill_colour = "SA2_NAME",
                   legend = FALSE, update_map_view = FALSE)
  })

  # observe check box
  observe({

    show_legend <- input$check
    my_fill_opacity <- as.integer(input$check)

    if(show_legend){
      google_map_update(map_id = "map") %>%
        update_polygons(data = melbourne, id = "polygonId",
                        fill_opacity = 1, fill_colour = "SA2_NAME",
                        legend = TRUE)
    } else {
      google_map_update(map_id = "map") %>%
        update_polygons(data = melbourne, id = "polygonId",
                        fill_opacity = 0, fill_colour = "SA2_NAME",
                        legend = FALSE)
    }

  })


}

shinyApp(ui, server)

The left picture is with the legend turned off at the start. The middle picture is after clicking "Fill polygons". The right picture is after unchecking "Fill polygons" - you can see the legend does not disappear. enter image description here

Vlad
  • 3,058
  • 4
  • 25
  • 53
  • @SymbolixAU - updated the repro to use the same data set and is hopefully more relevant to the question now. – Vlad Aug 31 '18 at 04:13
  • 1
    At the moment you can't turn off the legend, but I like the concept, so have added it as a [to do](https://github.com/SymbolixAU/googleway/issues/178). – SymbolixAU Aug 31 '18 at 04:24
  • If it is of help - the use case is census data and the fill colours represent different demographic measures. So having the legend change with the selected demographic is useful and also having it disappear when no demographic is selected. – Vlad Aug 31 '18 at 04:32

0 Answers0