Skip to content

Instantly share code, notes, and snippets.

@PietrH
PietrH / format_datetime.R
Created April 23, 2024 14:25
Format a datetime to a sensible string for filenames
format(Sys.time(), "%Y_%m_%d-%H_%M")
@PietrH
PietrH / paste1.R
Created April 23, 2024 12:58
paste0() but drop NA instead
str_replace_na <- function(string, replacement = ""){
string[is.na(string)] <- replacement
return(string)
}
c("a", NA, "c") |> str_replace_na(replacement = "") |> paste0(collapse = " ")
#> [1] "a c"
@PietrH
PietrH / anti_pattern_random_functions.R
Created April 15, 2024 12:34
Non repeatable results in R, even with seed set. Due to calls to random number generator by functions.
my_good_function <- function(seed = 789) {
invisible(withr::with_seed(seed, rnorm(1)))
}
my_bad_function <- function() {
invisible(rnorm(1))
}
# good pattern
set.seed(123)
good_first_rand <- rnorm(1) |> print()
@PietrH
PietrH / test-links-b3-dev-guide.R
Created March 8, 2024 14:36
Check and mine the links in a just the docs documentation website
@PietrH
PietrH / str_extract.R
Created February 26, 2024 08:50
str_extract in base R: extract a substring using regex
function(x, y) regmatches(x, regexpr(y, x))
@PietrH
PietrH / datapackage_to_tbl.R
Created February 12, 2024 14:56
frictionless datapackage.json to tibble (or csv)
datapackage_json <-
jsonlite::read_json("https://raw.githubusercontent.com/inbo/etn/main/inst/assets/datapackage.json")
datapackage_tbl <-
datapackage_json |>
purrr::chuck("resources") |>
purrr::map(
\(resource) purrr::set_names(
purrr::chuck(resource, "schema", "fields"),
purrr::chuck(resource, "name")
@PietrH
PietrH / check_presence.R
Created January 24, 2024 13:56
Check if a species has an occurrence in Belgium using rgbif
library(dplyr)
check_presence <-
function(scientificName = c('Vulpes vulpes', 'Pica pica'),
country = "BE") {
purrr::map(
scientificName,
~ rgbif::occ_data(
scientificName = .x,
country = country,
@PietrH
PietrH / query_gbif_example.md
Created January 24, 2024 13:29
Query gbif using rgbif::occ_data() but with multiple scientificNames at once
library(tidyverse)

# Example function of sending multiple scientificNames to rgbif::occ_data()
fetch_species_for_dimi <-
  function(scientificName = c("Vulpes vulpes", "Puma concolor"),
           country = "BE") {
    purrr::map(
      scientificName,
      ~ rgbif::occ_data(
@PietrH
PietrH / rename_with_based_on_df.R
Created November 13, 2023 12:24
Rename columns in a data.frame based on a second data.frame as a lookup table
first_df <- tibble::tibble(ids = c("id1", "id2", "id3"), labels = c("label1", "label2", "label3"))
second_df <- tibble::tibble(id1 = 1:3, id2 = 3:5, id3 = letters[6:8])
dplyr::rename_with(second_df, ~ first_df$labels[match(.x, first_df$ids)])
@PietrH
PietrH / package_dependencies.R
Created October 27, 2023 09:40
List R package dependencies using tools
tools::package_dependencies(packages = c("dplyr",
"tidyr",
"readr",
"stringr",
"purrr",
"here",
"tidylog",
"magrittr",
"sf",
"janitor",