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 / 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 / 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 / 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 / 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 / blank-slate-problem.R
Created April 5, 2023 14:19
`rm(list = ls())` probably doesn't do what you think it does
filter(head(presidents), rep(1, 3)) # this uses utils::filter
#> Time Series:
#> Start = 1
#> End = 6
#> Frequency = 1
#> [1] NA NA 244 220 188 NA
library(dplyr, warn.conflicts = FALSE) # attach dplyr
search() # shows dplyr will be preferred to utils if there's a nameclash
@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 / droplet-physics.R
Last active May 5, 2023 09:01
A bit of fun experimenting with tile-based 'gravity' in R
m <- matrix(rep(".", 30), 5, 6)
m[4, c(2:4, 6)] <- "X"
m[3, 2] <- "X"
m[5, 4:6] <- "X"
m[1, 4] <- "o"
matrix(1:140, 20, 7)
m <- pixeltrix::click_pixels(20, 7)
m[which(m == 0)] <- "."
m[which(m == 1)] <- "X"
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '%b '
setopt PROMPT_SUBST
PROMPT='%F{green}%*%f %F{blue}%~%f %F{red}${vcs_info_msg_0_}%f$ '
@matt-dray
matt-dray / petrov-nativity.R
Created December 27, 2022 17:44
Is Stiliyan Petrov the only Premier League player whose name contains the letters of the word 'nativity'?
# @optajoe on Twitter:
# Stiliyan Petrov is the only player to have played in the Premier
# League whose name contains all the letters in the word 'Nativity'.
# Here: https://twitter.com/OptaJoe/status/1607028528289030144
# Is that true? I decided to find out using my {soccercolleagues}
# package for R: https://www.rostrum.blog/2022/02/04/soccercolleagues/
install_github("matt-dray/soccercolleagues")
library(soccercolleagues)
@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()