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 / 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 = _) |>
@matt-dray
matt-dray / berthas-sadness.R
Created August 1, 2023 13:13
A stunning dual-y-axis chart to commemorate the reduction of people in the office called Bertha (not actual name) and the ensuing sadness.
library(ggplot2)
library(ggthemes)
library(extrafont)
font_import() # might take a minute
loadfonts(device = "win")
df <- data.frame(
Time = 1:3,
Berthas = 2:0,
Sadness = 0:2
@matt-dray
matt-dray / remove-massive-file-from-git.sh
Created July 21, 2023 20:00
Accidentally added a big file, Git refused to push, added a commit to remove the file, but it was still in the history, so needed to revert to prior commit, undo it but keep everything staged, then use git rm to remove the file, then re-add/commit push
git log --oneline
git reset dc871db
git log --oneline
git reset --soft HEAD~;
git status
git rm posts/2020-05-16-postcode-pandemonium/Data/NSPL21_FEB_2023_UK.csv
git status
git add .
git commit -m "Fix back to 2020-05-16 postcodes, rename folder dates, correct punctuation post, rebuild site"
git push origin md-fix-latest
@matt-dray
matt-dray / rectangularise-tables.R
Last active July 10, 2023 09:07
{officer} is an R package that lets you extract elements of a Word document, including tables, into a tidy dataframe. I've written a function to 're-rectangularise' an extracted Word table into an R dataframe.
# Functions building on the {officer} package:
# https://davidgohel.github.io/officer/
# You can read more about these functions in a blog post:
# https://www.rostrum.blog/2023/06/07/rectangular-officer/
# There are other solutions. You can also try {docxtractr} by Bob Rudis
# (on CRAN), which doesn't depend on {officer}, or {officerExtras} by Eli
# Pousson (on GitHub).
@matt-dray
matt-dray / abominable-namespacing.R
Created June 14, 2023 08:15
Everything's a function in R, including namespacing
`::`(dplyr, `%>%`)(mtcars, `::`(dplyr, select)(., cyl))
# Challenge for the reader: isn't the bracket a function too...?
@matt-dray
matt-dray / extract-word-tables.R
Last active June 9, 2023 16:31
Extract tables from Word files that are in different subfolders, then combine them.
# Extract and combine tables from multiple Word files
# This script creates some dummy docx files in temporary subfolders to mimic a
# user's filesystem. It then uses docxtractr::read_docx() to extract all the
# tables, and combines them with rbind().
# A follow-up to my blogpost:
# https://www.rostrum.blog/2023/06/07/rectangular-officer/
# Attach packages (all are available from CRAN)
@matt-dray
matt-dray / officer-scrape-word.R
Last active June 8, 2023 08:21
Basic use of the {officer} R package to extract a table from a Word document
# Basic use of the {officer} package to scrape a table from a Word doc
# https://davidgohel.github.io/officer/
# I've now written a couple of functions to do this task:
# https://gist.github.com/matt-dray/d4837f106bcee80ea39235b6465a7cac
# You can read more about those in a blog post:
# https://www.rostrum.blog/2023/06/07/rectangular-officer/
# There are other solutions. You can also try {docxtractr} by Bon Rudis
# (on CRAN), which doesn't depend on {officer}, or {officerExtras} by Eli
@matt-dray
matt-dray / two-table-a11ytables.R
Created April 25, 2023 08:21
Example of an {a11ytables} xlsx output that has two tables
# Example of an {a11ytables} xlsx output that has two tables
# 2023-04-25
# {a11ytables} v0.1.0
# Prepare cover table
cover_df <- tibble::tribble(
~subsection_title, ~subsection_content,
"Purpose", "Example results for something.",
"Workbook properties", "Some placeholder information.",
"Contact", "Placeholder email"
@matt-dray
matt-dray / ggplot2-manual-ticks.R
Last active May 5, 2023 09:30
Can you move tickmarks between bars on the categorical x-axis of a {ggplot2} barplot?
# From the Analysis Function's guidance on charts: 'When an axis shows
# categorical data, you do not necessarily need tick marks, but if you do use
# them labels should be aligned between them.' Is this actually possible in
# ggplot2? A quick example.
#
# Link to guidance:
# https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-3:~:text=on%20the%20chart.-,Tick%20marks,-Tick%20marks%20are
library(ggplot2)
@matt-dray
matt-dray / scrape-pla-obsidian.R
Last active May 5, 2023 09:16
Scrape for Pokemon locations in Obsidian Fieldlands in Pokemon Legends Arceus
library(rvest)
library(polite)
library(tidyr)
site <- "https://gamewith.net/"
obsidian_page <- paste0(site, "pokemon-legends-arceus/article/show/31334")
css_table <- ".pokela_poke td"
bow_site <- bow(site, "M Dray <https://www.matt-dray.com/>")