forked from CRWayman/USGS_Shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
174 lines (133 loc) · 6.16 KB
/
app.R
File metadata and controls
174 lines (133 loc) · 6.16 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
library(dataRetrieval)
library(dplyr)
library(leaflet)
library(plotly)
library(shinythemes)
library(shinycustomloader)
##########
# User interface ----
ui <- bootstrapPage(
theme= shinytheme("spacelab"),
titlePanel(title=div(img(src="nasa.jpg"), img(src="ssai.png"), "Exploring USGS Water Data",
style="z-index:1000;padding:0px 0px 0px 10px;"),
tags$head(tags$link(rel="shortcut icon", href="ssai.png"))),
navbarPage("Dashboard", id="nav",
tabPanel("Interactive map",
div(class="outer",
tags$head(
# Include our custom CSS
includeCSS("styles.css"),
includeScript("gomap.js")
),
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = "25%", left = 20, right = "auto", bottom = "auto",
width = 350, height = "auto", style = "z-index: 1;", ## z-index modification,
h3("USGS Hydro Data"),
tags$div(class = "header", checked = NA,
tags$b("Select Dates"),
br()
),
#textOutput("site"),
#uiOutput("Generate"),
sliderInput("date_slider", "",
min = as.Date("2020-01-01","%Y-%m-%d"),
max = as.Date("2020-12-31","%Y-%m-%d"),
value = c(as.Date("2020-01-01","%Y-%m-%d"), as.Date("2020-12-31","%Y-%m-%d"))),
#plotlyOutput("flowSeries", height = 200)
)),
fluidRow(tags$style(type = "text/css", "#map {height: calc(100vh) !important;}"),
withLoader(leafletOutput("mymap", width = "100%", height = "500px"), type="html", loader="loader1")),
br(),
fluidRow(plotlyOutput("flowSeries", height = 300))
),
tabPanel("Additional Information",
fluidRow(column(12,
h3('Placeholder Tab', style = "padding: 5px 10px 5px"),
br())))
)
)
server <- function(input, output, session) {
sites <- whatNWISsites(bBox=c(-83.0,36.5,-81.0,38.5),
parameterCd=c('00060','00010'),
hasDataTypeCd="dv")
site_nos = sites['site_no']
### First is taking daily data (average for the day since this site collect measurements every 15 mins)
Gage_daily <- readNWISdv(siteNumbers = c(site_nos[1])$site_no, #could input the site number that you want
parameterCd = c('00060','00010'), #Spc, Discharge, Gage height, temp
startDate = '2020-01-01', #I currently dont have an end date here which means it is going to keep looking for data
endDate = '2020-12-31') %>%
renameNWISColumns()
newGage <- merge(sites, Gage_daily, by="site_no")
newGage <- newGage[c("site_no", "station_nm", "dec_lat_va", "dec_long_va", "Date", "Flow", "Wtemp")]
# Clean Data
newGage <- na.omit(newGage)
newCoords <- cbind(newGage['dec_long_va'], newGage['dec_lat_va'])
points <- eventReactive(input$recalc, {
data.matrix(newCoords)
}, ignoreNULL = FALSE)
# Create custom icons
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = "darkblue")
clicked <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = "lightred")
# Render map
output$mymap <- renderLeaflet({
leaflet(data = newGage) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
# Centre the map
setView(lng = -82.0,
lat = 37.5,
zoom = 4) %>%
addAwesomeMarkers(data=points(), icon = icons)
#addMarkers(data = points())
})
observeEvent(input$mymap_marker_click, {
# Click point
p <- input$mymap_marker_click
# Create proxy map based on clicked point
proxy = leafletProxy("mymap") %>%
removeMarker(layerId="clicked") %>%
setView(lng = isolate(p$lng),
lat = isolate(p$lat),
zoom = 8) %>%
addAwesomeMarkers(lng = isolate(p$lng),
lat = isolate(p$lat),
layerId = "clicked",
icon = clicked)
# Subset Data Frame based on clicked point
pLat <- p$lat
plotGage <- subset(newGage, dec_lat_va == pLat)
# Find site name
site_name = unique(plotGage$station_nm)
# Produce Plotly figure
output$flowSeries=renderPlotly({
# Date Range
date_range <- input$date_slider
# Subset based on date range
plotGage <- subset(plotGage, Date>date_range[[1]] & Date < date_range[[2]]) %>%
arrange(desc(Date))
# Plotting
plot_ly(plotGage,
x=~Date,
y=~Flow,
size=~Wtemp,
name = "Water Temperature (C)",
mode = "lines+markers") %>%
layout(title=paste("Hydrological data for ", site_name),
yaxis = list(title = "Streamflow (cfs)"),
xaxis = list(title = "Date"),
legend = list(x = 0.75, y = 0.9),
showlegend=TRUE)
})
# Site Name
output$site <- renderText(paste("Showing data for", unique(plotGage$station_nm)))
})
}
# Run the app ----
shinyApp(ui = ui, server = server)