Skip to content

Instantly share code, notes, and snippets.

View matt-dray's full-sized avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
matt-dray / check-vector-pairs-multiples.R
Last active December 18, 2023 21:42
Checking pairs of vector inputs to an R function to see if the longer of each pair is a multiple of the shorter, emit a dynamic warning if so
View check-vector-pairs-multiples.R
pair_inputs <- function(...) {
args <- rlang::dots_list(..., .named = TRUE)
args_lengths <- lapply(args, length)
pairs <- combn(args_lengths, 2, simplify = FALSE)
failed <- lapply(pairs, function(x) max(unlist(x)) %% min(unlist(x)) != 0)
if (any(unlist(failed))) {
@matt-dray
matt-dray / check-type.R
Last active December 15, 2023 10:03
A base-R-only function to check argument inputs to another function, using dots to assess several of those inputs at once
View check-type.R
test_fn <- function(x = 1L, y = "x", z = 1.1) {
.check_type(x, y, z, type = "numeric")
cat("success")
}
.check_type <- function(..., type = "numeric") {
args <- list(...)
arg_names <- as.character(substitute(...()))
names(args) <- arg_names
@matt-dray
matt-dray / cr2rc.R
Last active December 9, 2023 22:30
Convert a spreadsheet cell reference (A1) to row-column (1, 1) format (overengineered draft)
View cr2rc.R
cr2rc <- function(cell_ref) {
ref_split <-
strsplit(cell_ref, "(?<=[[:alpha:]])(?=[[:digit:]])", perl = TRUE)[[1]]
if (length(ref_split) != 2) {
stop(
"You must supply a cell reference of 1 to 3 letters (the column reference) ",
"followed by a number (the row reference), like 'A1' or 'ABC100'.",
call. = FALSE
@matt-dray
matt-dray / inline-link-ad.R
Created October 26, 2023 20:38
We could put links to sponsor websites in the output from R functions, lol
View inline-link-ad.R
@matt-dray
matt-dray / spongebob-trivialpants.R
Created September 27, 2023 07:31
Deep-fried Sarcastic Spongebob on nontrivial package submission
View spongebob-trivialpants.R
# Regarding this toot: https://fosstodon.org/@coolbutuseless/111134523140682110
# Referring to this guidance: https://cran.r-project.org/web/packages/policies.html
# Using this deep-fry tech: https://www.rostrum.blog/posts/2021-11-07-deepfry/
library(magick)
library(spongebob)
fry <- function(
img_path, emoji_path,
text_top, text_bottom,
@matt-dray
matt-dray / cells-bilston.R
Created September 21, 2023 08:08
Freeing Brian Bilston's spreadsheet-based 'Cells' poem
View cells-bilston.R
# Freeing Brian Bilston's spreadsheet-based 'Cells' poem
# https://x.com/brian_bilston/status/1704061266979959099?s=46&t=QpOVBfS2sVIbcjn3ZLif3g
# Assuming the poem is saved to the file cells.xlsx
cells <- tidyxl::xlsx_cells("cells.xlsx", include_blank_cells = FALSE)
txt <- cells$character
txt_len <- length(txt)
author <- paste0("-", txt[txt_len])
txt <- txt[-txt_len]
lines <- split(txt, ceiling(seq_along(txt) / max(cells$col))) |> append(author)
@matt-dray
matt-dray / count-dupe-rows.R
Created August 22, 2023 19:08
Count the number of times that dulicate records appear in a dataframe
View count-dupe-rows.R
x <- data.frame(
col_a = c("A", "B", "A", "C", "A", "D", "B", "C"),
col_b = c(1, 2, 1, 3, 9, 4, 2, 9)
)
x
# col_a col_b
# 1 A 1
# 2 B 2
@matt-dray
matt-dray / get-categories.R
Last active August 21, 2023 07:53
Get all the categories from Quarto blog posts (between 'categories: ' and second instance of '---')
View get-categories.R
posts <-
list.files("posts", pattern = ".qmd", recursive = TRUE, full.names = TRUE)
get_categories <- function(post_path, ignore_rx = "resources") {
post_lines <- readLines(post_path, warn = FALSE)
cats_start <- which(post_lines == "categories:") + 1
cats_end <- which(post_lines == "---")[2] - 1
@matt-dray
matt-dray / bd2q-redirects.R
Last active August 26, 2023 18:23
Create a redirects file from {blogdown}-style URL paths to Quarto-style (for rostrum.blog)
View bd2q-redirects.R
redirect_to <- paste0("/", list.dirs("posts", recursive = FALSE))
date_rx <- "\\d{4}-\\d{2}-\\d{2}"
date_portion <- regexpr(date_rx, redirect_to) |>
regmatches(redirect_to, m = _) |>
gsub("-", "/", x = _)
name_portion <- gsub(paste0("posts/", date_rx, "-"), "", redirect_to)
@matt-dray
matt-dray / q2bd-blog-post-dirs.R
Last active August 16, 2023 21:25
Rearranging post directory structure for a Quarto blog to mimic the URL path of a {blogdown} blog (for rostrum.blog)
View q2bd-blog-post-dirs.R
paths <- list.dirs("posts", recursive = FALSE)
for (i in paths) {
from_dir <- i
date_rx <- "\\d{4}-\\d{2}-\\d{2}"
dates <- regexpr(date_rx, basename(i)) |>
regmatches(basename(i), m = _) |>