Skip to content

Instantly share code, notes, and snippets.

View PietrH's full-sized avatar

Pieter Huybrechts PietrH

View GitHub Profile
@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]+(?=\\))") %>%
@PietrH
PietrH / check_exported_fn_for_unit_tests.R
Created June 28, 2024 12:55
Do all exported functions have a corresponding testthat unit test file?
library(magrittr)
exported_functions <-
readr::read_lines("NAMESPACE") %>%
stringr::str_extract("(?<=export\\().+(?=\\))") %>%
.[!is.na(.)]
unit_tests <-
list.files("tests/testthat/") %>%
stringr::str_extract("(?<=test-).+(?=\\.R)") %>%
.[!is.na(.)]
@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,