owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
25 September 2025
Welcome!
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```
library(tidyverse)
```
(*you can copy paste this example and add your code further down*)
## Yellow sticky notes
No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge (see below).
## Participants
Name | Challenges
--- | ---
Damiano Oldoni | ***
Cécile Herr |
Ward Langeraert |
Emma Cartuyvels| *
Charlotte Van Moorleghem |
## Challenge 1
### Damiano's solution (example)
Copy paste this section to show your solutions.
```r
# dummy code
print("This is how to insert code.")
```
### Ward's solution
```r
## 1.1 ####
names(data_nl) <- species_df$species
names(data_nl)
## 1.2 ####
map(data_nl, function(taxon) {
nrow(taxon$data)
})
## 1.3 ####
map_int(data_nl, function(taxon) {
nrow(taxon$data)
})
## 1.4 ####
map(data_nl, function(taxon) {
taxon$data
}) %>%
list_rbind() # why not bind_rows?
## 1.5 ####
countries <- c("NL", "AT", "ES", "DK")
get_gbif_occurrences <- function(taxon_keys, country_code) {
rgbif::occ_search(
taxonKey = taxon_keys,
country = country_code,
year = "1950,2025",
hasCoordinate = TRUE,
occurrenceStatus = "PRESENT",
limit = 100000 # High enough
)
}
data_countries <- map(
countries,
get_gbif_occurrences,
taxon_keys = species_df$taxonKey
)
data_nl_og <- rgbif::occ_search(
taxonKey = species_df$taxonKey,
country = "NL",
year = "1950,2025",
hasCoordinate = TRUE,
occurrenceStatus = "PRESENT",
limit = 100000 # High enough
)
waldo::compare(data_countries[[1]], data_nl_og)
## 1.6 ####
# The input was not named
names(data_countries) <- countries
# Also rename second level
data_countries <- map(data_countries, function(country){
names(country) <- species_df$species
country
})
head(data_countries$NL$`Procyon lotor`$data)
```
### Emma's solution
```
## 1.1 ####
names(data_nl) <- c("Procyon lotor", "Nasua nasua", "Muntiacus reevesi")
## 1.2 ####
map(data_nl, function(x) nrow(x$data))
## 1.3 ####
map_int(data_nl, function(x) nrow(x$data))
## 1.4 ####
map(data_nl, "data") |> bind_rows()
## 1.5 and 1.6 ####
countries <- c("NL" = "NL",
"AT" = "AT",
"ES" = "ES",
"DK" = "DK")
retrieve_data <- function(country){
sub_dat <- rgbif::occ_search(
taxonKey = species_df$taxonKey,
country = country,
year = "1950,2025",
hasCoordinate = TRUE,
occurrenceStatus = "PRESENT",
limit = 100000 # High enough
)
names(sub_dat) <- c("Procyon lotor", "Nasua nasua", "Muntiacus reevesi")
return(sub_dat)
}
data_countries <- map(countries, retrieve_data)
```
## Challenge 2
### Ward's solution
```r
## 2.1 ####
spec_records <- map(data_countries, function(country){
map_vec(country, function(taxon) {
data <- taxon$data
if (is.null(data)) return(0)
nrow(data)
})
})
spec_records
## 2.2 ####
reduce(spec_records, sum)
## 2.3 ####
plot_records <- function(data, country, species) {
if (is.null(data)) return(paste("No data for", species, "in", country))
p <- ggplot2::ggplot(data = data) +
ggplot2::geom_bar(ggplot2::aes(x = year)) +
ggplot2::xlab("Year") +
ggplot2::ylab("Number of records") +
ggplot2::ggtitle(paste(species, country, sep = " - "))
return(p)
}
map(data_list, function(country) {
map(country, function(taxon) {
data <- taxon$data
species <- unique(data$species)
country <- unique(data$countryCode)
plot_records(data, species, country)
})
})
# Use for loop (much cleaner)
plot_records2 <- function(data_list, country, species) {
plot_data <- data_list[[country]][[species]]$data
if (is.null(plot_data)) return(paste("No data for", species, "in", country))
p <- ggplot2::ggplot(data = plot_data) +
ggplot2::geom_bar(ggplot2::aes(x = year)) +
ggplot2::xlab("Year") +
ggplot2::ylab("Number of records") +
ggplot2::ggtitle(paste(species, country, sep = " - "))
return(p)
}
for (country in names(data_list)) {
for (species in names(data_list[[country]])) {
print(plot_records2(data_list, country, species))
}
}
## 2.4 ####
data_path <- file.path("src", "20250925")
map(data_list, function(country) {
map(country, function(taxon) {
data <- taxon$data
if (!is.null(data)) {
key <- unique(data$speciesKey)
species <- unique(data$species)
country <- unique(data$countryCode)
file_name <- paste0(
"20250925_gbif_", key, "_", species, "_", country, ".csv"
)
paste0("Writing '", file_name, "'.")
write_csv(data, file.path(data_path, file_name))
}
})
})
## 2.5 ####
data_countries_df <- map(data_list, function(country) {
map(country, function(taxon) {
if (!is.null(taxon$data)) return(taxon$data)
})
}) %>%
list_c() %>%
list_rbind()
head(data_countries_df)
```
## Challenge 3