Skip to content

Instantly share code, notes, and snippets.

View PietrH's full-sized avatar

Pieter Huybrechts PietrH

View GitHub Profile
@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
@PietrH
PietrH / cli_glue_plural_expressions.R
Created November 21, 2024 13:55
Combine cli and glue syntax via rlang envs
cli::cli_abort("Friend{?s}: {zwoog}",
.envir = rlang::new_environment(
data = list(zwoog = c("Luc","Tendi")
)
)
)
@PietrH
PietrH / get_opera_overview.R
Last active November 27, 2024 09:28
Reading the OPERA radar overview json file, without jsonlite
httr2::request("https://raw.githubusercontent.com/enram/aloftdata.eu/refs/heads/main/_data/OPERA_RADARS_DB.json") |>
httr2::req_perform() |>
httr2::resp_body_json(check_type = FALSE) |>
purrr::map(~dplyr::tibble(
number = .x$number,
country = .x$country,
countryid = .x$countryid,
oldcountryid = .x$oldcountryid,
wmocode = .x$wmocode,
odimcode = .x$odimcode,
@PietrH
PietrH / rmi_radar_reprex.md
Last active November 19, 2024 15:32
Reading CROW radar Vertical Profile of Birds (VPB) data from the RMI using data.table in R
library(magrittr)
rmi_url <- "https://opendata.meteo.be/ftp/observations/radar/vbird/denhb/2024/denhb_vpts_20240128.txt"

header <- readr::read_lines(rmi_url) %>% 
  .[stringr::str_starts(.,"#")] %>%
  tail(1) %>% 
  stringr::str_remove("#") %>% 
  stringr::str_split(" ", simplify = TRUE) %>%
  .[nchar(.) > 0]
@PietrH
PietrH / check_similar_fn_args.R
Created July 8, 2024 10:04
check for similarly named function arguments in an R package
Library(dplyr)
# Which package to check
trias_namespace <- asNamespace("trias")
# Character vector of all functions and their arguments, print method of base ls with str() on every object
trias_fns_args <- capture.output(utils::lsf.str(trias_namespace))
# Extract the functions
@PietrH
PietrH / wkt_round_coordinates.R
Created July 1, 2024 12:28
Round coordinates in WKT strings
round_coordinates <- function(wkt_string, digits = 4) {
wkt_object_type <- wkt_string %>%
stringr::str_extract(".*?(?=\\()")
x_coord <- wkt_string %>%
stringr::str_extract("(?<=POINT\\()[0-9]+\\.[0-9]+") %>%
as.numeric()
y_coord <- wkt_string %>%
stringr::str_extract("[0-9]+\\.[0-9]+(?=\\))") %>%