Skip to content
Snippets Groups Projects
Select Git revision
  • 2e7325dff213b66b983917e9d032e28ec171e02c
  • master default
  • apiTutorial
  • gitResources
  • gisExample
  • syllabusDev
6 results

example_leaflet_reactive.R

Blame
  • Forked from SERG / woRkflow
    Source project has a limited visibility.
    example_leaflet_reactive.R 1.01 KiB
    library(shiny)
    library(leaflet)
    library(sf)
    
    # NC counties - a shapefile shipped with the sf package
    shape <- sf::st_read(system.file("shape/nc.shp", 
                                     package ="sf"),
                         stringsAsFactors = F) 
    
    
    # Define UI 
    ui <- fluidPage(
      
      # Application title
      titlePanel("Go Tar Heels!"),
      
      # Top panel with county name
      verticalLayout(
        
        wellPanel(textOutput("cnty")),
        
        # the map itself
        mainPanel(
          leafletOutput("map")
        )
      )
    )
    
    # Define server logic 
    server <- function(input, output) {
      
      output$map <- renderLeaflet({
        leaflet() %>% 
          addProviderTiles("Stamen.Toner") %>% 
          addPolygons(data = shape, 
                      fillColor = "aliceblue", 
                      color = "grey",
                      layerId = ~CNTY_ID)
      })
      
      # this is the fun part!!! :)
      observe({ 
        event <- input$map_shape_click
        output$cnty <- renderText(shape$NAME[shape$CNTY_ID == event$id])
        
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)