Skip to content

Instantly share code, notes, and snippets.

View PietrH's full-sized avatar

Pieter Huybrechts PietrH

View GitHub Profile
@PietrH
PietrH / no_connection.arg.R
Created January 23, 2026 09:11
Example of etn without connection argument
library(etn)
# Creating a connection object will result in a warning and not result in a
# connection object
my_con <- connect_to_etn()
#> Warning: `connect_to_etn()` was deprecated in etn 2.3.0.
#> ℹ You will be prompted for credentials instead.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
@PietrH
PietrH / connection_deprecated.R
Created January 23, 2026 09:04
Example of etn with deprecated connection argument
library(etn)
# Creating a connection object will result in a warning and not result in a
# connection object
my_con <- connect_to_etn()
#> Warning: `connect_to_etn()` was deprecated in etn 2.3.0.
#> ℹ You will be prompted for credentials instead.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
class(my_con)
@PietrH
PietrH / example.R
Created October 16, 2025 13:27
Species that are unique to a single GBIF dataset, which dataset has the most?
# Finding species that are only a single dataset and nowhere else: human
# observations edition
if(file.exists("query_results/0024071-251009101135966.zip")) {
species_per_dataset <-
readr::read_delim("query_results/0024071-251009101135966.zip")
} else {
species_per_dataset <-
rgbif::occ_download_get("0024071-251009101135966") |>
rgbif::occ_download_import()
@PietrH
PietrH / example.R
Created October 16, 2025 13:04
Occurrences of Passerines in South America, 3 most common genera
# Passerines in South America
# Data read from GBIF occurrence download (SQL QUERY)
pas_sa <-
rgbif::occ_download_get("0028483-251009101135966") |>
rgbif::occ_download_import() |>
dplyr::filter(!is.na(month))
# Identify the 3 most common genera -------------------------------------
common_genera <- pas_sa |>
@PietrH
PietrH / rato_dato.R
Created October 9, 2025 09:00
Get RATO data as flat table
# Get all rato data as a flat csv
if(!rlang::is_installed("ratatouille", version = "1.0.4")){
pak::pak("inbo/ratatouille")
}
library(ratatouille)
# To run this function you need to store your RATO database credentials as env variables, as RATO_USER and RATO_PWD, otherwise you'll get an error.
all_rato_data <- ratatouille()
@PietrH
PietrH / return_default_value.R
Created August 11, 2025 11:42
Get default value for a function argument in R
return_default <- function(my_param = 50){
rlang::eval_bare(formals(rlang::caller_fn(0))[["my_param"]])
}
@PietrH
PietrH / object_name_message.R
Created June 26, 2025 10:31
Use object name (symbol) in cli message in R
object_name_message <- function(x){
cli::cli_inform("The object you passed is called {substitute(x)}")
}
object_name_message(letters)
@PietrH
PietrH / create_zenodo_release_versions.R
Created April 17, 2025 13:30
Add old github repo releases to Zenodo
repo <- "my_org/my_repo"
zenodo_token <- "token from url in webhook page of repo"
repo_response <-
httr2::request("https://api.github.com/repos") %>%
httr2::req_url_path_append(repo) %>%
httr2::req_perform() %>%
httr2::resp_body_json()
release_response <-
@PietrH
PietrH / ripple_shuffle.R
Last active March 31, 2025 14:04
Ripple shuffle a vector in R so the order is: [first, last, second, second to last ...]
ripple_shuffle <- function(object) {
purrr::map(
seq(1, length(object) / 2),
~ c(
purrr::chuck(object, .x),
purrr::chuck(object, length(object) - .x + 1)
)
) %>%
unlist()
}
@PietrH
PietrH / create_inat_leaflet_map.R
Last active January 30, 2025 15:02
Example usage of iNaturalist API to create leaflet map for a taxon within radius of a point
get_inat_obs_radius <- function(taxon_id, lat, lng, radius) {
api_response <- httr2::request(
"https://api.inaturalist.org/v1/observations/"
) %>%
httr2::req_url_query(
taxon_id = taxon_id,
lat = lat,
lng = lng,
radius = radius
) %>%