-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard_5219.R
174 lines (139 loc) · 7.3 KB
/
dashboard_5219.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
list.of.packages <- c("shiny", "shinydashboard", "raster", "rgdal", "sp", "leaflet", "leaflet.extras", "geojsonio", "shinythemes","markdown", "ggplot2", "DT", "shinyWidgets")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
library(shiny)
library(shinydashboard)
library(raster)
library(rgdal)
library(sp)
library(leaflet)
library(leaflet.extras)
library(geojsonio)
library(markdown)
library(ggplot2)
library(dplyr)
library(DT)
library(shinyWidgets)
fm <- geojsonio::geojson_read("data/global_guides_r.geojson", what = "sp")
fg.data <- read.csv("data/FG_Category_Counry_cleaned_3.12.2019.csv")
ui <- dashboardPage(#skin = "black",
dashboardHeader(titleWidth = 400, title = "Field Museum Field Guides"),
dashboardSidebar(
sidebarMenu(style = "position: fixed; overflow: visible;"),
width = 400,
br(),
helpText("Click a desired country on the map for more information, or choose a country from the dropdown menu below", align = "center"),
selectInput(inputId = "variableselected", label = "Select country", choices = fm$admin),
plotOutput("PieChart"),
h2(align = "center",
img(src = "logo.png", height = 70, width = 70),
img(src = "shiny.png", height = 70, width = 120))
),
dashboardBody(
tags$head(tags$style(HTML('
/* active selected tab in the sidebarmenu */
.skin-blue .main-sidebar .sidebar .sidebar-menu .active a{
background-color: #DCDCDC;
color: #000000
}
/* other links in the sidebarmenu */
.skin-blue .main-sidebar .sidebar .sidebar-menu a{
background-color: #FFFFFF;
color: #000000;
}
/* logo */
.skin-blue .main-header .logo {
background-color: #0000FF;
}
/* navbar (rest of the header) */
.skin-blue .main-header .navbar {
background-color: #FFFAFA;
}
/* main sidebar */
.skin-blue .main-sidebar {
background-color: #FFFFFF;
}
/* body */
.content-wrapper, .right-side {
background-color: #FFFAFA;
}
'))),
fluidPage(
tags$style(type = "text/css", "#map {height: calc(100vh - 420px) !important;}"),
leafletOutput("map"),
DT::dataTableOutput("link_list")
)
)
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
bins <- c(1, 5, 100, 300)
#pal <- colorBin("Blues", domain = fm$guides_no, bins = bins) #YlGrn
pal <- colorBin(c("lightskyblue1","lightskyblue3", "dodgerblue3"), domain = fm$guides_no, bins = bins) #YlGrn
#labels <- sprintf(
# "<strong>%s</strong>", fm$admin) %>% lapply(htmltools::HTML)
#"<strong>%s</strong><br/>No. of Guides: <strong>%g</strong><sup></sup><br>Link:<strong><a href = %f></a>GO</strong></br>",
# fm$admin, fm$guides_no, fm$guide_link) %>% lapply(htmltools::HTML)
m <- leaflet(fm, options = leafletOptions(minZoom = 3, maxZoom = 4)) %>%
setView(7, 17, 2.5) %>%
addTiles(group = "OSM") %>%
#addProviderTiles(providers$Stamen.TonerBackground, group = "Black & White") %>%
addProviderTiles(providers$OpenTopoMap, group = "Topography") %>%
addProviderTiles("Esri.WorldImagery", group = "Satellite")
m %>% addPolygons(
fillColor = ~pal(guides_no),
weight = 1,
opacity = 0.8,
color = "black",
fillOpacity = 0.8,
group = "Field Guides",
popup = paste("<h4>", "<strong>", fm$admin, "</strong> <br> No. of Guides: <strong>", fm$guides_no,
"</strong> <br> Link: <a href = ", fm$guide_link, "> Go to guides </a>"),
highlight = highlightOptions(
weight = 2.5,
color = "Yellow",
fillOpacity = 1,
bringToFront = TRUE)) %>%
#label = labels,
#labelOptions = labelOptions(
# style = list("font-weight" = "normal", padding = "3px 6px"),
# textsize = "12px",
# direction = "auto")) %>%
addLayersControl(
baseGroups = c("OSM", "Topography", "Satellite"),
overlayGroups = c("Field Guides"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(pal = pal, values = ~guides_no, title = "Number of Field Guides",
position = "bottomright", opacity = 0.9) %>%
addResetMapButton() %>%
addSearchFeatures(
targetGroups = c("Field Guides"),
options = searchFeaturesOptions(
zoom=18, openPopup = FALSE, firstTipSubmit = FALSE,
autoCollapse = TRUE, hideMarkerOnCollapse = FALSE ))
})
output$PieChart<-renderPlot({
fg.data %>%subset(Countries == input$variableselected) %>%
select(Countries, Category) %>%
group_by(Category) %>%
summarise(count=n()) %>%
mutate(Proportion = count/sum(count))%>%
ggplot(aes(x="", y=Proportion, fill=Category))+ geom_bar(width = 1, stat = "identity")+ coord_polar("y", start=0)+
#scale_fill_manual(values = c("Plants" ="seagreen2", "Birds" = "steelblue4", "Fishes"="steelblue",
# "Herp" = "powerderblue", "Insects" = "darkslategray3","Mammals" = "cornsilk4",
# "Fungi"= "steelblue1", "Other" = "palevioletred2")) +
scale_fill_manual(values = c("Plants" ="cornsilk4", "Birds" = "steelblue4", "Fishes"= "steelblue",
"Herp" = "powderblue", "Insects" = "darkslategray3","Mammals" = "dodgerblue",
"Fungi"= "steelblue1", "Other" = "cornsilk2")) +
theme_bw() + theme(legend.text=element_text( colour = "black", size=rel(1.2)))
})
output$link_list <- DT::renderDataTable({
fg.data %>%subset(Countries == input$variableselected) %>%
dplyr::select(-category,-country,-state, -language, -date_created) %>%
dplyr::mutate(URL = paste0("https://fieldguides.fieldmuseum.org/guides/guide/", guide_no)) %>%
dplyr::mutate(title = paste0("<a href='", URL, "'>",guide_title,"</a>")) %>%
dplyr::select(title, guide_no, page_no,Category) %>%
dplyr :: rename(Title = title, Guide_No = guide_no,Page_Number = page_no)
}, escape = FALSE)
}
shinyApp(ui = ui, server = server)