-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
130 lines (104 loc) · 4.01 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
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
# Automatically loads required packages if the user doesn't have it already
list.of.packages <- c("shiny", "raster", "rgdal", "sp", "leaflet", "geojsonio", "markdown")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
library(shiny)
library(raster)
library(rgdal)
library(sp)
library(leaflet)
library(geojsonio)
library(markdown)
######################
### LOAD VARIABLES ###
######################
#wd <- setwd("C:/Shiny/Field_Guides_Leaflet")
# Import shape variables
#countries <- readOGR("C:/Shiny/Field_Guides_Leaflet/data", "global_bounds")
fm <- geojsonio::geojson_read("data/fm_guides_r.geojson", what = "sp")
##########
### UI ###
##########
ui <- fluidPage(
id = "main_content",
titlePanel("Field Guides Dashboard"),
tags$style(type = "text/css", "#map {height: calc(100vh - 20px) !important;}"),
leafletOutput("map"),
absolutePanel(
top = "auto", left = 20, right = "auto", bottom = 200,
width = 330, height = "auto", draggable = TRUE, fixed = TRUE,
wellPanel(
HTML(markdownToHTML(fragment.only=TRUE, text=c(
"Search `Field Guides` by country"
))),
selectInput(inputId = "variableselected", label = "Select country",
choices = fm$admin),
textOutput("selected_var")
),
style = "opacity: 0.90"
)
)
#sidebarLayout(
# sidebarPanel(
# selectInput(inputId = "variableselected", label = "Select country",
# choices = fm$admin)),
#mainPanel(
# leafletOutput("map", width = 1000, height = 600)
#)
#)
################
#### SERVER ####
################
server <- function(input, output, session) {
output$selected_var <- renderText({
paste("You have selected", input$variableselected)
})
output$map <- renderLeaflet({
# Global color scheme
bins <- c(1, 5, 50, 250)
pal <- colorBin("Blues", domain = states$FG_Numb, bins = bins)
# Add labels
labels <- sprintf(
"<strong>%s</strong><br/><strong>%g</strong> Field Guides<sup></sup><br>Link:<br/>",
fm$admin, fm$FG_Numb) %>% lapply(htmltools::HTML)
m <- leaflet(fm, height = "100%", options = leafletOptions(minZoom = 1, maxZoom = 5)) %>%
setView(7, 17, 1.5) %>%
addTiles(group = "OSM") %>%
addProviderTiles(providers$Stamen.TonerBackground, group = "Black & White")
#addProviderTiles("MapBox", options = providerTileOptions(
# id = "mapbox.light",
# accessToken = Sys.getenv('sk.eyJ1IjoibmtvdGxpbnNraSIsImEiOiJjanRyYnN6NGYwb20zNDBwY3M0Nmtpa2Y4In0.fvRJSU66OfBBQd9aSsZIqQ')))
# accessToken = Sys.getenv('pk.eyJ1IjoibmtvdGxpbnNraSIsImEiOiJjanRyYnJteHUwb2JhNDRtdWR4bHRiem9uIn0.wtil2ENruTPGdwliR5p7bg')))
# Run the map
m %>% addPolygons(
fillColor = ~pal(FG_Numb),
weight = 1,
opacity = 1,
color = "black",
#dashArray = "3",
fillOpacity = 1,
group = "Field Guides",
highlight = highlightOptions(
weight = 2,
color = "Yellow",
#dashArray = "",
fillOpacity = 1,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 6px"),
textsize = "12px",
direction = "auto")) %>%
addLayersControl(
baseGroups = c("OSM", "Black & White"),
overlayGroups = c("Field Guides"),
options = layersControlOptions(collapsed = FALSE)
) %>%
addLegend(pal = pal, values = ~FG_Numb, title = "Number of Field Guides",
position = "bottomleft")
})
}
#########################
#### RUN APPLICATION ####
#########################
shinyApp(ui = ui, server = server)