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 / gitalias.txt
Created October 9, 2019 14:34
Git aliases
alias.hist=log --decorate --oneline --graph --all
@matt-dray
matt-dray / allequal-tibble-dataframe.R
Last active October 19, 2019 00:02
Testing comparisons between data.frame and tibble objects with all.equal()
# all.equal() behaves differently when presented with objects of class
# data.frame versus those with tibble class. This is important for
# trying to rewrite the {tidyr} episode of Software Carpentry's
# R for Reproducible Scientific Analysis lesson to include the new
# pivot_wider() and pivot_longer() columns instead of spread() and
# gather(), which have been superseded.
# Seems to be a noted problem:
# https://github.com/tidyverse/tibble/issues/287
@matt-dray
matt-dray / DATADIR.sh
Created October 21, 2019 11:04
DATADIR location
cd /Users/matthewdray/.bash_profile
@matt-dray
matt-dray / regex-example.py
Last active October 21, 2019 12:24
Basic re module usage to find text strings
# Import re for working with regular expressions
import re
# Provide some text to search
# 'Hipster ipsum' via https://hipsum.co/
text = "Lorem ipsum dolor amet selfies williamsburg tattooed ethical wolf cloud bread. Cronut marfa heirloom pour-over. Jean shorts aesthetic before they sold out, yr subway tile kale chips occupy. Banjo lomo af, meditation roof party cronut vape glossier."
# Search for a specific phrase
# Returns a 'match' object
# More info here: https://www.w3schools.com/python/python_regex.asp#matchobject
@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 / screenshot-gif-rstudio.R
Created December 23, 2019 12:08
Use {magick} to read 'before' and 'after' images to a gif
library(magick)
before <- image_read("~/Desktop/before.png")
before <- image_crop(before, "815x300-80-200")
after <- image_crop(image_read("~/Desktop/after.png"))
after <- image_crop(after, "815x300-80-200")
# To morph
# frames <- image_morph(c(before, after, before), frames = 20)
@matt-dray
matt-dray / tidyr-expand.R
Created March 9, 2020 12:04
Using `tidyr::expand()` to ensure all variable combinations are included
library(dplyr)
library(tidyr)
data <- tibble(
var1 = c(rep("A", 3), rep("B", 2)),
var2 = c(letters[1:3], letters[1:2]),
var3 = runif(5)
)
expand(data, var1, var2) %>%
@matt-dray
matt-dray / coolanduseful.R
Last active April 6, 2020 08:06
Using {ggpattern} to plot plots inside a plot
# Plot weight by cylinders, with weight by gear inside
# Using {ggpattern} by @coolbutuseless
# https://coolbutuseless.github.io/package/ggpattern/
# Tongue-in-cheek blog post: https://www.rostrum.blog/2020/04/05/yo-dawg/
# Load packages
library(dplyr)
library(purrr)
library(tidyr)
library(ggplot2)
@matt-dray
matt-dray / vars_setnames.R
Created April 6, 2020 09:24
Problem: using the content of vars() in set_names() after a map()
# Question: how can content of vars() be supplied to set_names() after a map()?
# Also: below, can variables_bare be coerced to variables_quoted, or vice-versa?
# Load packages
suppressPackageStartupMessages(library(dplyr))
library(purrr)
# Quoted and unquoted vectors
variables_bare <- vars(Sepal.Width, Petal.Length) # to be passed to .x in map()
variables_quoted <- c("Sepal.Width", "Petal.Length") # as above, quoted version
@matt-dray
matt-dray / small-crosstalk-test.Rmd
Last active April 27, 2020 15:11
Different plot types linked with {crosstalk}
---
title: "Crosstalk test"
output:
html_document:
code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(crosstalk)