2
votes

I have a leaflet map and I want the option of switching from the values of A being mapped to the values of B. Every example I can find says to use shiny and leaflet and all of these examples include something along the lines of:

ui <- fluidPage(
  selectInput(inputId = "Data", 
              label = "Data", 
              choices = c("A","B"),
  leafletProxy(outputId = "map") #or leafletOutput
))

but I keep getting the error that

leafletProxy (or leafletOutput)does not exist

. How do I solve this? My leaflet is created with :

mypal <- colorNumeric(palette = "viridis", domain = d$A)

    leaflet() %>% 
      addProviderTiles("OpenStreetMap.Mapnik") %>%
      setView(lat = 39.8283, lng = -98.5795, zoom = 4) %>%
      addPolygons(data =

USA, stroke = TRUE, color='black', opacity=1, weight=.5, smoothFactor = 0.2, fillOpacity = 1,
                  fillColor = ~mypal(d$A),
                  popup = paste('<b>',d$state, "</b><br>A:", d$A) %>%
      addLegend(position = "bottomleft", pal = mypal, values = d$A,
                title = "A",
                opacity = 1)
1

1 Answers

0
votes

It seems from your example that your shiny has no server function, so it is not going to work.

Please, find attached a mock shiny you can start building on:

library(shiny)
library(leaflet)

ui <- fluidPage(
  selectInput(inputId = "Data", 
              label = "Data", 
              choices = c("A", "B")),
  leafletOutput("map")
  )

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

  output$map <- renderLeaflet({
                              if((input$Data) == "A"){
                                point = c(42.6525, -73.757222)
                              } 
                              if((input$Data) == "B"){
                                point = c(39.283333, -76.616667)
                              } 
    
    
                            leaflet() %>% 
                             addProviderTiles("OpenStreetMap.Mapnik") %>%
                              addMarkers(lat = point[1], lng = point[2])
  })
  

It will show "Albany" when you select "A" and Baltimore when you select "B"

Basically:

ui is kind of the "interface", what it is going to be shown:

  1. selectInput: you can choose A or B here

  2. leafletOutput: will show the leaflet map

server will do the "hard job" of creating the map and computing actions when you use selecInput:

output$map means that we want to render the leafletOuput (that is why it is called map, as in leafletOutput("map")

Then, according to the input selected (A or B)

 if((input$Data) == "A"){
                          point = c(42.6525, -73.757222)
                        } 
                        if((input$Data) == "B"){
                          point = c(39.283333, -76.616667)
                        } 

We assign coordinates of Albany or Baltimore to point. Finally, we build the map:

leaflet() %>% 
   addProviderTiles("OpenStreetMap.Mapnik") %>%
   addMarkers(lat = point[1], lng = point[2])

PLEASE, take into account that this is a mock shiny, it is far from perfect, it is only illustrative.

Best!