Chapter 8 Interactive Maps with Shiny
8.1 Learning Objectives
- Create a basic Shiny app using
leaflet
8.2 Topics Learned
- Shiny (UI vs. server)
- Interactivity
Find the Shiny cheatsheet in RStudio under Help > Cheatsheets > Web Applications with shiny
8.3 Overview
This workshop teaches how to create a basic Shiny web app using leaflet.
8.4 Interactive Tutorial
This workshop’s Shiny app code can be found here.
Challenge
Create a new Shiny app from a template in R. Run the app. Which parts of the Shiny UI code map to the app? How are
ui
andserver
linked (what are the features that are the same across both?)Change the title of the app.
Challenge
- In the UI object, add a
leafletOutput("map")
call in themainPanel()
function. Then, in the server object, add aoutput$map <- renderLeaflet({})
call.
That is, fill in the following script:
ui <- fluidPage(
# Application title
titlePanel("World Population Over Time"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
# sliderInput("bins",
# "Number of bins:",
# min = 1,
# max = 50,
# value = 30),
),
# Specifies what to put in the main panel
mainPanel(
# Put one line of code here
)
)
)
server <- function(input, output) {
# output$distPlot <- renderPlot({
# # generate bins based on input$bins from ui.R
# x <- faithful[, 2]
# bins <- seq(min(x), max(x), length.out = input$bins + 1)
#
# # draw the histogram with the specified number of bins
# hist(x, breaks = bins, col = 'darkgray', border = 'white')
# })
output$map <- renderLeaflet({
# Put three lines of leaflet code here
})
}
Challenge
- In the UI object, add a
sliderInput
of"year"
. Change the step size to 5, and remove the comma for thousands (hint: do?sliderInput
to look at the documentation, and options).
Challenge
- Create a new variable called
pop_per_year
that is a subset of city by year, depending on which year you enter (input$year
). Use thefilter()
command in thedplyr
package.
Challenge
Try resizing the marker size depending on population, adding a popup, or doing more to customize your map!
Try adding a feature in your app so that you only show cities over a certain population in millions (specified by the user), using
numericInput()
instead ofsliderInput()
.
Challenge
- Add a data table element with
renderDataTable()
anddataTableOutput()
so you can see the attributes of the points in the map.
You can share your Shiny apps publicly by creating an account at https://shinyapps.io and clicking Publish in the top of your app script.
8.5 R Training Workshop
I will be teaching a day-long “R for Social Scientists” Data Carpentry workshop on April 12 at the Center for Spatial Data Science.
Topics to be covered include:
- Introduction to R
- Working with data types, strings, and dates in R
- Manipulating data frames in R
- Data visualization in R
…and lunch will be provided!
Please register at this link if you are interested!
8.6 Links
- Leaflet documentation website: https://rstudio.github.io/leaflet/
- Intro to Shiny webinar (45 minutes): https://www.rstudio.com/resources/webinars/introduction-to-shiny/
- Free Shiny online course from Datacamp: https://www.datacamp.com/courses/building-web-applications-in-r-with-shiny
- Shiny examples:
- Generate random points on a map: https://bhaskarvk.github.io/user2017.geodataviz/presentations/07-Interactive_Maps.html#36
- Zip code explorer: http://shiny.rstudio.com/gallery/superzip-example.html
- Twin cities bus dashboard: https://gallery.shinyapps.io/086-bus-dashboard/