forked from mikejohnson51/leaflet-intro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshiny-starter.Rmd
141 lines (103 loc) · 2.83 KB
/
shiny-starter.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
---
title: "Shiny/Leaflet"
subtitle: "Building a USA water dashboard"
output:
html_document:
toc: true
toc_float: true
toc_collapsed: true
---
```{r,echo=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE, comment = "#>", out.width = "100%")
```
# Libraries
```{r}
library(AOI) # Data and geocoding
library(dataRetrieval) # USGS data access
library(sf) # all things spatial ...
library(dplyr) # data manipulations ...
library(leaflet) # mapping
```
# Data
### County `sf` object
```{r}
counties = aoi_get(state = 'conus', county = "all") %>%
mutate(location = paste(name, state_abbr, sep = ", ")) %>%
select(geoid, name, state_name, location)
glimpse(counties)
plot(counties$geometry)
```
### Active USGS gages
```{r}
sites = readRDS("shiny/usgs_sites.rds")
glimpse(sites)
```
# Core Leaflet functions
### Auto generate basemap
```{r}
basemap = function(){
leaflet() %>%
addProviderTiles('CartoDB.Positron') %>%
setView(lat = 39, lng = -95, zoom = 3)
}
```
#### Example
```{r}
basemap()
```
### Zoom to a county, given a GEOID
```{r}
zoom_to_county = function(map, counties, FIP){
shp = filter(counties, geoid == FIP)
bounds = as.vector(st_bbox(shp))
clearGroup(map, 'shp') %>%
addPolygons(data = shp,
color = "#003660",
fillColor = "#FEBC11",
fillOpacity = .2,
group = "shp") %>%
flyToBounds(bounds[1], bounds[2], bounds[3], bounds[4])
}
```
#### Example
```{r}
zoom_to_county(basemap(), counties, 25017)
```
# USGS functions
### Wrapper for finding streamflow given a siteID
```{r}
get_streamflow = function(siteID){
readNWISdv(siteNumbers = siteID,
parameterCd = '00060',
startDate = Sys.Date() - 365) %>%
renameNWISColumns()
}
```
#### Example
```{r}
flow = get_streamflow('01098530')
glimpse(flow)
```
### [Network Linked Data Index](https://usgs-r.github.io/dataRetrieval/articles/nldi.html) function given a lat/lon
```{r}
find_nldi = function(x, y){
findNLDI(location = c(x,y), nav = c('UM'),
find = c("basin", "flowlines"), distance = 1000)
}
```
#### Example
```{r}
nldi_out = find_nldi(-104.780837, 38.786796)
basemap() %>%
addPolygons(data = nldi_out$basin) %>%
addPolygons(data = nldi_out$UM_flowlines) %>%
zoom_to_county(counties, '08041')
```
# Where are we going?
We will use [flexdashboard](https://rmarkdown.rstudio.com/flexdashboard/) to generate the app.
If you know Rmarkdown basics, this will be intuitive for you!
The key new piece is that:
- Each Level 1 Header (#) begins a new page in the dashboard.
- Each Level 2 Header (##) begins a new column/row.
- Each Level 3 Header (###) begins a new box.
The app material can be found [here](https://github.com/mikejohnson51/leaflet-intro/tree/master/shiny)