File size: 3,769 Bytes
526ed7e
5c9a01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92562ea
5c9a01f
 
 
 
 
 
 
 
 
92562ea
 
5c9a01f
 
 
 
 
 
 
92562ea
5c9a01f
 
 
 
 
e98e810
5c9a01f
 
526ed7e
 
5c9a01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81645ef
5c9a01f
 
 
 
81645ef
 
5c9a01f
 
 
 
81645ef
5c9a01f
 
 
81645ef
 
 
5c9a01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8710e5
dcdfe4c
 
5c9a01f
 
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
175
library(shiny)
library(bs4Dash)
library(leaflet)
library(highcharter)
library(reactable)
library(reactablefmtr)
library(data.table)
library(dplyr)
library(lubridate)


# load map function
source("Map.R")

# load plot function
source("Plot.R")

# load table function
source("Table.R")

# load dataset
bio_poland <- fread("poland.csv")

# filter dataset and select relevant columns
poland_bio <- bio_poland |>
  filter(vernacularName != "") |>
  select(
    vernacularName,
    longitudeDecimal,
    latitudeDecimal,
    individualCount,
    eventDate,
    locality
  ) |>
  mutate(eventDate = as_date(eventDate))


# add blanks to vector of choices so that the widget loads blank
choices2 <- unique(poland_bio$vernacularName)


# create dashboard brand
dash_title <- dashboardBrand(
  title = "Biodiversity in Poland",
  color = "primary",
  href = "https://www.gbif.org/occurrence/search?q=Poland&country=PL",
  image = "bird_2.jpg"
)

# define UI
ui <- dashboardPage(
  title = "Biodiversity",
  scrollToTop = T,
  help = NULL,
  dark = NULL,
  header = dashboardHeader(title = dash_title),
  sidebar = dashboardSidebar(disable = T),
  footer = dashboardFooter(left = "Source: Global Biodiversity Informational Facility"),
  body = dashboardBody(
    tags$style(HTML("
    .box {
      border: 2px solid #007bff !important;
      box-shadow: none !important;
    }
  ")),
    fluidRow(
      column(
        12,
        selectizeInput("vernacular",
          label = strong("Vernacular Name"),
          choices = choices2,
          selected = "Mandarin Duck",
          width = "100%"
        )
      )
    ),
    fluidRow(
      box(
        class = "box",
        title = "Map of Observations",
        maximizable = TRUE,
        width = 12,
        status = "primary",
        solidHeader = T,
        leafletOutput("map_plot"), elevation = 4
      )
    ),
    fluidRow(
      box(
        class = "box",
        title = "Plot of Observation Overtime",
        maximizable = TRUE,
        status = "primary",
        solidHeader = T,
        highchartOutput("chart"), elevation = 4
      ),
      box(
        class = "box",
        title = "Observation Data",
        maximizable = TRUE,
        status = "primary",
        solidHeader = T,
        reactableOutput("table", height = 400),
        elevation = 4
      )
    )
  ),
  fullscreen = T
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  # make input values reactive and return the defaults if both input values are NULL
  input_vernacular <- reactive({
    input$vernacular %||% "Mandarin Duck"
  })


  # render map
  output$map_plot <- renderLeaflet({
    req(poland_bio)
    tryCatch(
      {
        mapData(
          df = poland_bio,
          vernacular = input_vernacular()
        )
      },
      error = function(e) {
        leaflet() |>
          addTiles() |>
          addPopups(lng = 0, lat = 0, popup = "No data available for the selected filters")
      }
    )
  })

  # render chart
  output$chart <- renderHighchart({
    req(poland_bio)
    tryCatch(
      {
        plotData(
          df = poland_bio,
          vernacular = input_vernacular()
        )
      },
      error = function(e) {
        highchart() |>
          hc_title(text = "No data available for the selected filters")
      }
    )
  })

  # render table
  output$table <- renderReactable({
    req(poland_bio)
    tryCatch(
      {
        tableFunction(
          df = poland_bio,
          vernacular = input_vernacular()
        )
      },
      error = function(e) {
        reactable(data.frame(
          Message = "No data available for the selected filters"
        ))
      }
    )
  })
}

# Run the application
shinyApp(ui = ui, server = server)