shiny_leaflet_app

Enrique Figueroa Navarro
02/10/2021

Introduction

  • The application shows how we can use Shiny for geolocating some organizations.
  • It is based on code from “Using Leaflet with Shiny” https://rstudio.github.io/leaflet/shiny.html
  • The app shows some Peruvian universities located in Lima, the capital of Perú.
  • A csv file provides basic data used in a leaflet map: - name - latitud - longitud - founded

How it works: UI

  • It is a single application, meaning the shiny ui and server side are encoded in a single file
  • The ui part uses 85% of the absolute panel for the map and a slider.
  • The slider features the date range of when some universities were founded.
  • A textbox mirrors the date range selected.

How it works: server side

  • First, the df is filtered to determine the date range in a reactive function.

    filteredData = reactive({universities[universities$founded >= input$range[1] & universities$founded <= input$range[2],] })

  • The map is rendered via a leaflet instruction:

    output$map = renderLeaflet({ leaflet(universities) %>% addTiles() %>% fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat)) })

  • Finally, an observe instruction is used to show changes to the slider:

    observe({ leafletProxy(“map”, data = filteredData()) %>% clearShapes() %>% addCircles(radius = ~104/60, weight = 12, color = “blue”, # WORKING fillOpacity = 0.1, popup = ~paste(universities$university) ) })

Instructions

Basic instructions are displayed at he bottom of the app:

  • Some Peruvian universities geolocations are shown as a blue circle, according to the date when they were founded.

  • Clicking on any of the blue circles displays the name of the university.

  • The slider allows to filter the universities. Also, the date range of the slider is mirrored in a texbox.

Link to the code

Complete code can be found at:

https://github.com/efignav/shiny_leaflet_app