-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_map_app.R
218 lines (175 loc) · 5.04 KB
/
table_map_app.R
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copied from https://github.com/tknoch8/travistats_blog/blob/master/table_map_app/table_map_app.R
# repo for run app function is at
# https://github.com/tknoch8/linked_datatable_leaflet_app.git
devtools::load_all('C:/Users/matt.landis/OneDrive - Resource Systems Group, Inc/Git/tmrtools')
require(tidyverse)
require(shiny)
require(shinydashboard)
require(datasets)
require(DT)
require(leaflet)
# Define scales for map
pal = colorNumeric(get_rsg_palette('hot'),
domain=c(min(quakes$mag), max(quakes$mag)))
rad_scale = function(mag){
sizemin = 5
sizemax = 15
sizerange = sizemax - sizemin
datarange = max(quakes$mag) - min(quakes$mag)
scale = sizerange / datarange
radius = (mag - min(quakes$mag)) * scale
return(radius)
}
# ui
my_sidebar <- dashboardSidebar(
width = 250,
sidebarMenu(
id = "menu_1",
br(),
actionButton(
"select_all_rows_button",
"Select All Table Rows"
),
br(),
actionButton(
"clear_rows_button",
"Clear Table Selections"
),
br(),
sliderInput(
inputId="mag",
label = "Magnitude",
min=min(quakes$mag),
max=max(quakes$mag),
value = c(min(quakes$mag), max(quakes$mag)),
step = 0.1)
)
)
my_header <- dashboardHeader(title = "Fiji Earthquakes")
my_body <- dashboardBody(
fluidRow(
box(
width = 6,
height=750,
solidHeader = TRUE,
leafletOutput(
"my_leaflet",
height=700
)
),
box(
width = 6,
height=750,
solidHeader = TRUE,
DTOutput(
"my_datatable",
height=700
)
)
) # fluidRow
) # dashboardBody
my_ui <- dashboardPage(
header = my_header,
sidebar = my_sidebar,
body = my_body
)
my_server <- function(session, input, output) {
quakes_r <- reactive({
quakes %>%
as_tibble() %>%
filter(
mag > input$mag[1],
mag < input$mag[2]
)
})
output$my_datatable <- renderDT({
quakes_r() %>%
datatable(
rownames=FALSE,
extensions="Scroller",
style="bootstrap",
class=c("compact", "display"),
width="100%",
fillContainer=TRUE,
options=list(
deferRender=TRUE,
scrollY=300,
scroller=TRUE,
searching=FALSE))
})
# base map that we will add points to with leafletProxy()
output$my_leaflet <- renderLeaflet({
leaflet() %>%
addProviderTiles(
provider = providers$CartoDB.Positron,
options = providerTileOptions(
noWrap = FALSE
)
) %>%
setView(
lat = -25.5,
lng = 178.58,
zoom = 4
)
})
# Set map to show selected rows
observeEvent(input$my_datatable_rows_selected, {
selected_lats <- eventReactive(input$my_datatable_rows_selected, {
as.list(quakes_r()$lat[c(unique(input$my_datatable_rows_selected))])
})
selected_longs <- eventReactive(input$my_datatable_rows_selected, {
as.list(quakes_r()$long[c(unique(input$my_datatable_rows_selected))])
})
selected_depths <- eventReactive(input$my_datatable_rows_selected, {
as.list(quakes_r()$depth[c(unique(input$my_datatable_rows_selected))])
})
selected_mags <- eventReactive(input$my_datatable_rows_selected, {
as.list(quakes_r()$mag[c(unique(input$my_datatable_rows_selected))])
})
selected_stations <- eventReactive(input$my_datatable_rows_selected, {
as.list(quakes_r()$stations[c(unique(input$my_datatable_rows_selected))])
})
# this is the data that will be passed to the leaflet in the addCircleMarkers argument,
# as well as the popups when the points are hovered over
map_df <- reactive({
tibble(lat = unlist(selected_lats()),
lng = unlist(selected_longs()),
depth = unlist(selected_depths()),
mag = unlist(selected_mags()),
stations = unlist(selected_stations()))
})
leafletProxy("my_leaflet", session) %>%
clearMarkers() %>%
addCircleMarkers(
data = map_df(),
lng = ~lng,
lat = ~lat,
stroke = FALSE,
color = ~pal(mag),
radius = ~rad_scale(mag),
fillOpacity = 0.8,
popup = paste0(
"depth: ", map_df()$depth, "<br>",
"mag: ", map_df()$mag, "<br>",
"stations: ", map_df()$stations))# %>%
#addLegend(pal=pal, values=~mag, opacity=0.8)
})
# create a proxy to modify datatable without recreating it completely
DT_proxy <- dataTableProxy("my_datatable")
# clear row selections when clear_rows_button is clicked
observeEvent(input$clear_rows_button, {
selectRows(DT_proxy, NULL)
})
# clear markers from leaflet when clear_rows_button is clicked
observeEvent(input$clear_rows_button, {
clearMarkers(leafletProxy("my_leaflet", session))
})
# select all rows when select_all_rows_button is clicked
observeEvent(input$select_all_rows_button, {
selectRows(DT_proxy, input$my_datatable_rows_all)
})
}
shinyApp(
ui = my_ui,
server = my_server
)