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 / openxlsx-auto-font-test.R
Last active November 13, 2023 14:56
A test of how automatic font colour is handled in {openxlsx} (it isn't) and {openxlsx2} (it is)
library(openxlsx)
font_name <- "Comic Sans MS"
sheet_name <- "font_test"
font_null <- createStyle(fontName = font_name)
font_black <- createStyle(fontName = font_name, fontColour = "black")
font_red <- createStyle(fontName = font_name, fontColour = "red")
row_seq <- 1:nrow(beaver1)
wb_openxlsx <- createWorkbook()
@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
@matt-dray
matt-dray / write-qr.R
Last active October 24, 2023 10:24
Write a QR code to disk with {qrcode} and `png()`
png(
"~/Desktop/qr_test.png",
width = 5,
height = 5,
units = "cm",
res = 1200
)
qrcode::qr_code("https://www.matt-dray.com") |> plot()
@matt-dray
matt-dray / spongebob-trivialpants.R
Created September 27, 2023 07:31
Deep-fried Sarcastic Spongebob on nontrivial package submission
# 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
# 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 / 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)
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 / count-dupe-rows.R
Created August 22, 2023 19:08
Count the number of times that dulicate records appear in a dataframe
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 '---')
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 / extract-html-data-leaflet.R
Last active August 16, 2023 21:25
Using R to extract data out of some HTML code for a leaflet map (needed for a blogdown to Quarto blog conversion for rostrum.blog)
x <- readLines("~/Desktop/leaflet-map.txt")
popup_html <- stringr::str_split_1(x, "\",\"")
lmb_simple <- tibble::tibble(
status_id = stringr::str_extract(popup_html, "\\d{19}"),
lat = stringr::str_extract(popup_html, "(?<=📍 )5\\d{1}\\.\\d{0,4}(?=, )"),
lon = stringr::str_extract(popup_html, "(?<=\\d, )(-)?\\d\\.\\d{0,4}(?=<br>📮)"),
osm_url = glue::glue("https://www.openstreetmap.org/#map=17/{lat}/{lon}/"),
media_url = stringr::str_extract(popup_html, "(?<=img src=\\')http://pbs\\.twimg\\.com/media/.*\\.jpg(?=\\' width)")
@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)
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 = _) |>