Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
@@ -1,65 +1,171 @@
|
|
1 |
library(shiny)
|
2 |
-
library(
|
3 |
-
library(
|
4 |
-
library(
|
5 |
-
library(
|
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 |
-
server
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
})
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
tryCatch(
|
47 |
{
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
},
|
52 |
-
error = function(e){
|
53 |
-
|
|
|
54 |
}
|
55 |
)
|
56 |
})
|
57 |
-
|
58 |
-
#
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
})
|
62 |
}
|
63 |
|
64 |
-
|
65 |
-
|
|
|
1 |
library(shiny)
|
2 |
+
library(bs4Dash)
|
3 |
+
library(leaflet)
|
4 |
+
library(highcharter)
|
5 |
+
library(reactable)
|
6 |
+
library(reactablefmtr)
|
7 |
+
library(data.table)
|
8 |
+
library(dplyr)
|
9 |
+
library(lubridate)
|
10 |
+
|
11 |
+
|
12 |
+
# load map function
|
13 |
+
source("Map.R")
|
14 |
+
|
15 |
+
# load plot function
|
16 |
+
source("Plot.R")
|
17 |
+
|
18 |
+
# load table function
|
19 |
+
source("Table.R")
|
20 |
+
|
21 |
+
# load dataset
|
22 |
+
bio_poland <- fread("poland.csv")
|
23 |
+
|
24 |
+
# filter dataset and select relevant columns
|
25 |
+
poland_bio <- bio_poland |>
|
26 |
+
filter(vernacularName != "") |>
|
27 |
+
select(
|
28 |
+
vernacularName,
|
29 |
+
longitudeDecimal,
|
30 |
+
latitudeDecimal,
|
31 |
+
individualCount,
|
32 |
+
eventDate,
|
33 |
+
locality
|
34 |
+
) |>
|
35 |
+
mutate(eventDate = as_date(eventDate))
|
36 |
+
|
37 |
+
|
38 |
+
# add blanks to vector of choices so that the widget loads blank
|
39 |
+
choices2 <- unique(poland_bio$vernacularName)
|
40 |
+
|
41 |
+
|
42 |
+
# create dashboard brand
|
43 |
+
dash_title <- dashboardBrand(
|
44 |
+
title = "Biodiversity in Poland",
|
45 |
+
color = "primary",
|
46 |
+
href = "https://www.gbif.org/occurrence/search?q=Poland&country=PL",
|
47 |
+
image = "bird_2.jpg"
|
48 |
+
)
|
49 |
+
|
50 |
+
# define UI
|
51 |
+
ui <- dashboardPage(
|
52 |
+
title = "Biodiversity",
|
53 |
+
scrollToTop = T,
|
54 |
+
help = NULL,
|
55 |
+
dark = NULL,
|
56 |
+
header = dashboardHeader(title = dash_title),
|
57 |
+
sidebar = dashboardSidebar(disable = T),
|
58 |
+
footer = dashboardFooter(left = "Source: Global Biodiversity Informational Facility"),
|
59 |
+
body = dashboardBody(
|
60 |
+
tags$style(HTML("
|
61 |
+
.box {
|
62 |
+
border: 2px solid #007bff !important;
|
63 |
+
box-shadow: none !important;
|
64 |
+
}
|
65 |
+
")),
|
66 |
+
fluidRow(
|
67 |
+
column(
|
68 |
+
12,
|
69 |
+
selectizeInput("vernacular",
|
70 |
+
label = strong("Vernacular Name"),
|
71 |
+
choices = choices2,
|
72 |
+
selected = "Mandarin Duck",
|
73 |
+
width = "100%"
|
74 |
+
)
|
75 |
+
)
|
76 |
+
),
|
77 |
+
fluidRow(
|
78 |
+
box(
|
79 |
+
class = "box",
|
80 |
+
title = "Map of Observations",
|
81 |
+
width = 12,
|
82 |
+
status = "primary",
|
83 |
+
solidHeader = T,
|
84 |
+
leafletOutput("map_plot"), elevation = 4
|
85 |
+
)
|
86 |
+
),
|
87 |
+
fluidRow(
|
88 |
+
box(
|
89 |
+
class = "box",
|
90 |
+
title = "Plot of Observation Over time",
|
91 |
+
status = "primary",
|
92 |
+
solidHeader = T,
|
93 |
+
highchartOutput("chart"), elevation = 4
|
94 |
+
),
|
95 |
+
box(
|
96 |
+
class = "box",
|
97 |
+
title = "Observation Data",
|
98 |
+
status = "primary",
|
99 |
+
solidHeader = T,
|
100 |
+
reactableOutput("table", height = 400),
|
101 |
+
elevation = 4
|
102 |
+
)
|
103 |
)
|
104 |
+
),
|
105 |
+
fullscreen = T
|
106 |
)
|
107 |
|
108 |
+
# Define server logic required to draw a histogram
|
109 |
+
server <- function(input, output, session) {
|
110 |
+
# make input values reactive and return the defaults if both input values are NULL
|
111 |
+
input_vernacular <- reactive({
|
112 |
+
input$vernacular %||% "Mandarin Duck"
|
113 |
+
})
|
114 |
+
|
115 |
+
|
116 |
+
# render map
|
117 |
+
output$map_plot <- renderLeaflet({
|
118 |
+
req(poland_bio)
|
119 |
+
tryCatch(
|
120 |
+
{
|
121 |
+
mapData(
|
122 |
+
df = poland_bio,
|
123 |
+
vernacular = input_vernacular()
|
124 |
+
)
|
125 |
+
},
|
126 |
+
error = function(e) {
|
127 |
+
leaflet() |>
|
128 |
+
addTiles() |>
|
129 |
+
addPopups(lng = 0, lat = 0, popup = "No data available for the selected filters")
|
130 |
+
}
|
131 |
+
)
|
132 |
})
|
133 |
+
|
134 |
+
# render chart
|
135 |
+
output$chart <- renderHighchart({
|
136 |
+
req(poland_bio)
|
137 |
tryCatch(
|
138 |
{
|
139 |
+
plotData(
|
140 |
+
df = poland_bio,
|
141 |
+
vernacular = input_vernacular()
|
142 |
+
)
|
143 |
},
|
144 |
+
error = function(e) {
|
145 |
+
highchart() |>
|
146 |
+
hc_title(text = "No data available for the selected filters")
|
147 |
}
|
148 |
)
|
149 |
})
|
150 |
+
|
151 |
+
# render table
|
152 |
+
output$table <- renderReactable({
|
153 |
+
req(poland_bio)
|
154 |
+
tryCatch(
|
155 |
+
{
|
156 |
+
tableFunction(
|
157 |
+
df = poland_bio,
|
158 |
+
vernacular = input_vernacular()
|
159 |
+
)
|
160 |
+
},
|
161 |
+
error = function(e) {
|
162 |
+
reactable(data.frame(
|
163 |
+
Message = "No data available for the selected filters"
|
164 |
+
))
|
165 |
+
}
|
166 |
+
)
|
167 |
})
|
168 |
}
|
169 |
|
170 |
+
# Run the application
|
171 |
+
shinyApp(ui = ui, server = server)
|