-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
58 lines (50 loc) · 1.68 KB
/
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
# Credit: build on the example in https://rstudio.github.io/leaflet/shiny.html
library(sf)
library(shiny)
library(spData)
library(leaflet)
library(tidyverse)
world_coffee = left_join(world, coffee_data)
pal = colorNumeric(palette = "RdYlBu", domain = c(0, 4000))
ui = fluidPage(
sidebarPanel(
sliderInput("range", "Coffee Production", 0, 4000,
value = c(1000, 4000), step = 100),
selectInput("year", "Year", c(2016, 2017)),
checkboxInput("legend", "Show legend", FALSE)
),
mainPanel(
leafletOutput("map")
)
)
server = function(input, output, session) {
map_centre = st_centroid(world %>% filter(name_long == "Brazil")) %>%
st_coordinates()
# This reactive expression returns a character string representing the selected variable
yr = reactive({
paste0("coffee_production_", input$year)
})
# Reactive expression for the data subset to what the user selected
filteredData = reactive({
world_coffee$Production = world_coffee[[yr()]]
filter(world_coffee, Production >= input$range[1] &
Production <= input$range[2])
})
output$map = renderLeaflet({
# Things that do not change go here:
leaflet() %>% addTiles() %>%
setView(lng = map_centre[, "X"], map_centre[, "Y"], zoom = 2)
})
# Changes to the map performed in an observer
observe({
proxy = leafletProxy("map", data = filteredData()) %>%
clearShapes()
# Show or hide legend
proxy %>% clearControls() %>% addPolygons(fillColor = ~pal(Production))
if (input$legend) {
proxy %>% addLegend(position = "bottomright",
pal = pal, values = ~Production)
}
})
}
shinyApp(ui, server)