January 17, 2017

Introductin: Shiny App

  • Shiny creates a web application framework for R users
  • Shiny allows reactive graphic interface for users to interact with visualizations
  • No prior knowledge of HTML, CSS or JavaScript
  • Complete online tutorial here

Introduction: Leaflet

  • Leaflet is one of the most popular Javascript libraries for creating interactive maps.
  • Is embeded in the leaflet R package
leaflet() %>%
addTiles() %>%
addMarkers(lat=40.778883, lng=-77.841371, 
           popup="University Park, State College")

Merge Shiny and Leaflet

  • Tutorials can be found here.
library(shiny)
shinyApp(
  ui <- fluidPage(
    leafletOutput("mymap", width = "80%", height = 300),
    actionButton("recalc", "New points")),

  server <- function(input, output, session) {
    points <- eventReactive(input$recalc, {
      cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
    }, ignoreNULL = FALSE)
    output$mymap <- renderLeaflet({
      leaflet() %>% addTiles() %>%
      addMarkers(data = points())
    })
  })

Shiny Map Finding MacDonalds in US

  • It is important to find a dining place

  • Input the zip code and miles from zip to find the nearest MacDonald

  • The distance is calculated using great circle distance measure

Let (lat, lng) to be \((\phi_1, \lambda_1)\) and \((\phi_2, \lambda_2)\)

The central angle \[\Delta\sigma=\text{arccos}(\text{sin}\phi_1\text{sin}\phi_2+\text{cos}\phi_1\text{cos}\phi_2\text{cos}(\Delta\lambda))\]

The arc length (also the great circle distance) \[d=r\Delta\sigma\]

Important Links