Skip to content

Instantly share code, notes, and snippets.

View PietrH's full-sized avatar

Pieter Huybrechts PietrH

View GitHub Profile
@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
) %>%
@PietrH
PietrH / get_etn_stats.R
Last active January 27, 2025 14:53
Get a number of stats for a few ETN projects
library(dplyr)
# Ensure ETN API-beta version
remotes::install_github("inbo/[email protected]")
library(etn)
# Get number of detections, species and animals for lifewatch presentation
project_codes_to_investigate <- c(
"2010_PHD_REUBENS",
"2011_RIVIERPRIK",
"2012_LEOPOLDKANAAL",
@PietrH
PietrH / find_allele_per_gene.R
Last active January 2, 2025 14:08
Find the closest gene per allele for every chromosome
# We are trying to find the closest allele per gene
## You could possibly do this with this package as well:
## https://bioconductor.org/packages/release/bioc/vignettes/GenomicRanges/inst/doc/GenomicRangesIntroduction.html#finding-the-nearest-genomic-position-in-granges-objects
library(dplyr) # A Grammar of Data Manipulation
library(assertthat) # Easy Pre and Post Assertions
# Some example data