August 26, 2018

Introduction

This app contains an interactive world map for educational purposes. By selecting a country, the app zooms into the country and provides state/provincial names and their respective plot area.

This app is developed in R and hosted via R Shiny. Data is sourced from Natural Earth.

App Site: slmf1995.shinyapps.io/geoanalytics

Tab: World Map

App Framework

Create a dropdown list to facilitate data collection.

## in server.R
output$select_ctry <- renderUI(
    selectInput('country', 'Select a country',
                choices = c('World', countries$admin),
                selectize = F)
  )

## in ui.R
uiOutput('select_ctry')

Reactive plot in response to data collection.

## in server.R
observeEvent(input$country, {
    if(input$country == 'World'){
      # clear polygon plot...
    } else {
      # plot selected country...
    }
  })

Plot script

## in ui.R, plot leaflet
leaflet(x$geometry) %>%
  addTiles(options = tileOptions(minZoom = 2)) %>%
  setMaxBounds(lng1 = -180, lat1 = -90, lng2 = 180, lat2 = 90) %>%
  fitBounds(lowlng, lowlat, highlng, highlat) %>% # to zoom into selected country
  addPolygons(color = x$color,
              popup = paste('<strong>Country:</strong>', x$admin, '<br>',
                            '<strong>Region:</strong>', x$name, '<br>',
                            '<strong>Plot Area:</strong>', x$value),
              weight = 1, opacity = 1,
              highlightOptions = highlightOptions(color = 'white', weight = 2,
                                                  bringToFront = T))

Sample Output