-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path08-leaflet-shiny.Rmd
156 lines (107 loc) · 4.49 KB
/
08-leaflet-shiny.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Interactive Maps with Shiny
## Learning Objectives
- Create a basic Shiny app using `leaflet`
## Topics Learned
- Shiny (UI vs. server)
- Interactivity
```{block type="rmdinfo"}
Find the Shiny cheatsheet in RStudio under Help > Cheatsheets > Web Applications with shiny
```
## Overview
This workshop teaches how to create a basic Shiny web app using leaflet.
## Interactive Tutorial
```{block type="rmdinfo"}
This workshop's Shiny app code can be found [here](https://github.com/spatialanalysis/workshop-scripts/blob/master/gis-visualization/winter-2019/R/leaflet-example/app.R).
```
```{block type="learncheck"}
**Challenge**
```
1. 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` and `server` linked (what are the features that are the same across both?)
2. Change the title of the app.
```{block type="learncheck"}
```
```{block type="learncheck"}
**Challenge**
```
3. In the UI object, add a `leafletOutput("map")` call in the `mainPanel()` function. Then, in the server object, add a `output$map <- renderLeaflet({})` call.
That is, fill in the following script:
```{r eval=FALSE}
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
})
}
```
```{block type="learncheck"}
```
```{block type="learncheck"}
**Challenge**
```
4. 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).
```{block type="learncheck"}
```
```{block type="learncheck"}
**Challenge**
```
5. 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 the `filter()` command in the `dplyr` package.
```{block type="learncheck"}
```
```{block type="learncheck"}
**Challenge**
```
6. Try resizing the marker size depending on population, adding a popup, or doing more to customize your map!
7. 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 of `sliderInput()`.
```{block type="learncheck"}
```
```{block type="learncheck"}
**Challenge**
```
8. Add a data table element with `renderDataTable()` and `dataTableOutput()` so you can see the attributes of the points in the map.
```{block type="learncheck"}
```
```{block type="rmdinfo"}
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.
```
## 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](https://www.eventbrite.com/e/r-for-social-scientists-data-carpentry-workshop-tickets-57131487818) if you are interested!
## 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/