January 11, 2017

Description of functionality

This application allows the user to select one of a set of five Boston historical landmarks and displays an interactive map of the landmark.

  • Radio button group with landmark selections is displayed in Sidebar panel
  • Leaflet Map is displayed in main panel
  • Marker is added at the coordinates of the selected location, with marker text displaying the name of the landmark along with an explanation of historical significance and/or trivia facts.
  • Distance of selected landmark to Downtown Crossing is displayed below map

Link: https://olga-smolyar.shinyapps.io/BostonLandmarks/

ui.R file Sidebar and Main panels

The below code defines a sidebar panel and adds a radio button group with the landmark selections. It sets the first radio button as the default selection.

    sidebarPanel(
    "Interesting facts about Boston Landmarks",
    radioButtons("selection", "Select a Landmark",
                       c("Copley Square" = "1",
                         "Downtown Crossing" = "2",
                         "Old South Meeting House" = "3",
                         "Faneuil Hall" = "4",
                         "Fenway Park" = "5"
                         )
                 ,selected="1")

The below code uses the output$colmap variable defined in Server.R to output a map:

  mainPanel(
h4("Click marker to view landmark facts"),
    leafletOutput("colmap"),
h4('Distance to Downtown Crossing (meters):'),
verbatimTextOutput("distance")

Server.R code explanation

In the below Server.R code, the output$colmap output variable is defined using the renderLeaflet function of the Leaflet package. Depending on the input radio button selected, a different set of inputs to Leaflet is provided.

shinyServer(
  function(input, output) {
    library(magrittr)
    library(geosphere)
    copley=c(-71.076261,42.349914)
    downtown=c(-71.0602, 42.355)
    oldsouth=c(-71.058356, 42.3570)
    faneuil=c(-71.0545975, 42.35998)
    fenway=c(-71.097196, 42.346502)
    
    output$colmap<-renderLeaflet({
      if (input$selection=="1"){
        output$distance=renderPrint(distm(copley, downtown, fun = distHaversine)[1,1])
        leaflet() %>%
         addTiles(options = providerTileOptions(noWrap = TRUE))  %>%
          addMarkers(lng=copley[1], lat=copley[2],popup="Copley Square: <br> Home to the Boston Public Library, <br> John Hancock Tower, <br> Trinity Church <br> and Old South Church historic sites")
      }
      else
  ...
    })
  }
)

Sample Leaflet Output

Sample Leaflet Output on Main Panel: