-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
71 lines (59 loc) · 2.83 KB
/
app.R
File metadata and controls
71 lines (59 loc) · 2.83 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
library(shiny)
library(leaflet)
library(RColorBrewer)
pal <- colorNumeric("Reds", c(min(quakes$mag)-0.5, max(quakes$mag)))
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10,
sliderInput("mags", "Magnitudes", min(quakes$mag), max(quakes$mag),
value = range(quakes$mag), step = 0.1
),
sliderInput("depths", "Depths", min(quakes$depth), max(quakes$depth),
value = range(quakes$mag), step = 1
),
checkboxInput("legend", "Show legend", TRUE),
htmlOutput("summary"),
plotOutput("plot"),
style = "background: rgba(255, 255, 255, 0.5); padding: 5px; border: 1px solid black"
)
)
server <- function(input, output, session) {
filteredData <- reactive({
quakes[quakes$mag >= input$mags[1] & quakes$mag <= input$mags[2] & quakes$depth >= input$depths[1] & quakes$depth <= input$depths[2],]
})
output$map <- renderLeaflet({
leaflet(quakes) %>% addTiles() %>%
fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
})
output$summary <- renderText({
paste("<label>Selected Data Summary</label><p>Average depth: ", round(mean(filteredData()$depth),2), " km<br>",
"Average magnitude:", round(mean(filteredData()$mag),1)," Richter</p>")
})
output$plot <- renderPlot({
plot(filteredData(), pch=20, lwd=.2, cex=.2)
})
observe({
leafletProxy("map", data = filteredData()) %>%
clearShapes() %>%
addCircles(radius = ~10^mag/10,
weight = 1,
color = "#777777",
fillColor = ~pal(mag),
fillOpacity = 0.7,
popup = ~paste0("<label>Event details</label><div>Magnitude: ", mag, " Richter<br>Depth: ", depth, " km</div>")
)
})
observe({
proxy <- leafletProxy("map", data = quakes)
proxy %>% clearControls()
if (input$legend) {
proxy %>% addLegend(position = "bottomright",
pal = pal,
values = ~mag,
title = "Richter"
)
}
})
}
shinyApp(ui, server)