-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
153 lines (136 loc) · 4.94 KB
/
app.R
File metadata and controls
153 lines (136 loc) · 4.94 KB
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
library(shiny)
library(ggplot2)
library(maps)
library(jsonlite)
# --- Kartdata (lasta ein gong) ---
norge_kart <- tryCatch(
map_data("worldHires", region = "Norway"),
error = function(e) map_data("world", region = "Norway")
)
# --- Hjelpefunksjonar ---
sok_art <- function(query) {
url <- paste0("https://api.gbif.org/v1/species/suggest?q=",
URLencode(query), "&limit=8&rank=SPECIES")
res <- tryCatch(fromJSON(url, simplifyVector = TRUE), error = function(e) NULL)
if (is.null(res) || nrow(res) == 0) return(data.frame())
res[, intersect(c("key", "scientificName", "vernacularName", "kingdom"), names(res))]
}
hent_gbif <- function(taxon_key, ar_fra = 2025) {
base <- paste0(
"https://api.gbif.org/v1/occurrence/search",
"?taxonKey=", taxon_key,
"&country=NO&year=", ar_fra, ",2026",
"&hasCoordinate=true&limit=300"
)
first <- tryCatch(fromJSON(paste0(base, "&offset=0")), error = function(e) NULL)
if (is.null(first)) return(data.frame())
total <- first$count
res <- first$results[, c("decimalLongitude", "decimalLatitude"), drop = FALSE]
if (total > 300) {
for (off in seq(300, min(total, 3000), by = 300)) {
page <- tryCatch(fromJSON(paste0(base, "&offset=", off)), error = function(e) NULL)
if (!is.null(page))
res <- rbind(res, page$results[, c("decimalLongitude", "decimalLatitude"), drop = FALSE])
}
}
res <- as.data.frame(res)
res[!is.na(res$decimalLongitude) & !is.na(res$decimalLatitude), ]
}
# --- UI ---
ui <- fluidPage(
titlePanel("Ferskvassfisk i Noreg \u2013 GBIF"),
sidebarLayout(
sidebarPanel(
width = 3,
textInput("sok", "S\u00f8k etter art:", placeholder = "t.d. Salmo trutta"),
actionButton("sok_btn", "S\u00f8k", class = "btn-primary"),
hr(),
uiOutput("resultat_ui"),
hr(),
sliderInput("ar_fra", "Observasjonar fr\u00e5 og med \u00e5r:",
min = 2000, max = 2025, value = 2025, step = 1, sep = ""),
actionButton("hent_btn", "Vis p\u00e5 kart", class = "btn-success"),
hr(),
uiOutput("info_ui")
),
mainPanel(
width = 9,
plotOutput("kart", height = "700px")
)
)
)
# --- Server ---
server <- function(input, output, session) {
sokeresultat <- reactiveVal(data.frame())
valt_art <- reactiveVal(NULL)
obs_data <- reactiveVal(data.frame())
observeEvent(input$sok_btn, {
req(nchar(trimws(input$sok)) > 1)
res <- sok_art(trimws(input$sok))
sokeresultat(res)
valt_art(NULL)
obs_data(data.frame())
})
output$resultat_ui <- renderUI({
res <- sokeresultat()
if (nrow(res) == 0) return(NULL)
valalternativ <- setNames(as.character(res$key), res$scientificName)
tagList(
selectInput("valt_key", "Vel art:", choices = valalternativ),
tags$small("Trykk 'Vis p\u00e5 kart' for \u00e5 hente data")
)
})
observeEvent(input$hent_btn, {
req(input$valt_key)
data <- hent_gbif(input$valt_key, input$ar_fra)
obs_data(data)
res <- sokeresultat()
namn <- res$scientificName[as.character(res$key) == input$valt_key]
valt_art(list(key = input$valt_key, namn = namn))
})
output$info_ui <- renderUI({
obs <- obs_data()
art <- valt_art()
if (is.null(art) || nrow(obs) == 0) return(NULL)
tagList(tags$b(art$namn), tags$br(),
tags$span(paste0(nrow(obs), " observasjonar")))
})
output$kart <- renderPlot({
obs <- obs_data()
art <- valt_art()
p <- ggplot() +
geom_polygon(
data = norge_kart,
aes(x = long, y = lat, group = group),
fill = "grey75", color = "grey40", linewidth = 0.3
) +
coord_quickmap(xlim = c(4, 32), ylim = c(57, 72)) +
scale_x_continuous(breaks = seq(4, 32, by = 4),
labels = paste0(seq(4, 32, by = 4), "\u00b0\u00d8")) +
scale_y_continuous(breaks = seq(58, 72, by = 2),
labels = paste0(seq(58, 72, by = 2), "\u00b0N")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, size = 9, color = "grey40"),
panel.background = element_rect(fill = "white", color = NA),
plot.background = element_rect(fill = "white", color = NA),
panel.grid.major = element_line(color = "grey85", linewidth = 0.2),
panel.grid.minor = element_blank(),
axis.text = element_text(size = 8, color = "grey40"),
axis.title = element_blank()
)
if (!is.null(art) && nrow(obs) > 0) {
p <- p +
geom_point(data = obs,
aes(x = decimalLongitude, y = decimalLatitude),
color = "#2ca02c", size = 2.5, alpha = 0.75) +
labs(title = art$namn,
subtitle = paste0("GBIF \u2013 Noreg \u2013 n = ", nrow(obs), " observasjonar"))
} else {
p <- p + labs(title = "S\u00f8k etter ein art og trykk 'Vis p\u00e5 kart'")
}
p
})
}
shinyApp(ui, server)