Synoposis

This document is for the submission for Developing Data Products - Week 2 Assignment. The project is for creating a webpage using R markdown that features a map created with Leaflet.

Interactive Map

I have created an interactive map on using Leaflet and shiny packages. The map displays the cities from any of the selected countries USA, India and China and shows the cities with population between the range selected by slider.

The map consists of several components. The first component is the map loaded using leaflet. There is a section at the bottom of the map that includes the slider. The slider has the range from 1,000,000 to 10,000,000. This slider is used to select the population size. Only cities with selected population are displayed on the map. Another component of this interactive map is the radio button list. This list is being used for selecting countries. The combination of Country and Population range are used as an input and the selected cities are displayed on the map.

Let’s start with loading necessary packages

library(leaflet)
library(maps)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(shiny)

I am using dataset world embedded in r for this assignment.

data(world.cities)

Next code is to setup the UI page.

ui <- fluidPage(
  titlePanel("Population in Cities by Country"), 
  leafletOutput("mymap"),
  fluidRow(column(2,
                  sliderInput("slider", h3("Select population range"), 1000000, 10000000, 1000)),
                  radioButtons("radio", h3("Select country"),
                               choices = list("USA"="USA", "India"="India", "China"="China"), selected="USA")
           
           )
)
## Warning: In sliderInput(): `value` should be greater than or equal to `min`
## (value = 1000, min = 1e+06).

Below code is written to setup a server page

server <- function(input, output, session){
  output$mymap <- renderLeaflet({
    
    leaflet(world.cities %>%
          dplyr::filter (
            country.etc == input$radio,
            pop > input$slider
          )) %>%
    addTiles() %>%
    addMarkers(lat=~lat, lng=~long, popup=~name, clusterOptions = markerClusterOptions())
    }
  )
}

The final step is to call the shiny app.

shinyApp(ui, server)
## 
## Listening on http://127.0.0.1:8045