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 / save-many-plots-pwalk.R
Created February 25, 2019 14:26
Create a nested dataframe, with a column for the data in a tibble object; a plot object created from the data tibble; and a filename
# https://rstudio-pubs-static.s3.amazonaws.com/294422_099df24a1c0f46f99db848ca5c48ff6b.html
library(tidyverse)
plots <- mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(plot = map(data, ~ggplot(., aes(mpg, wt)) + geom_point()),
filename = paste0(cyl, ".pdf")) %>%
select(filename, plot)
@matt-dray
matt-dray / union-polygons-sf.R
Created February 26, 2019 09:26
Example of a geospatial union between polygons with R's {sf} package
# Example of a geospatial union between polygons with R's {sf} package
# Combine East and West Midlands to 'Midlands'
# This example uses a GeoJSON of super-generalised clipped boundaries for England's regions:
# http://geoportal.statistics.gov.uk/datasets/regions-december-2017-super-generalised-clipped-boundaries-in-england
# (click the API dropdown on this page and copy the URL for the GeoJSON)
# Packages
library(geojsonio)
library(sf)
@matt-dray
matt-dray / all-the-memes.R
Created April 16, 2019 08:47
Save all the base meme images from the {memer} package
devtools::install_github("sctyner/memer")
library(memer)
library(tidyverse)
library(magick)
memes <- tibble(meme_name = meme_list()) %>%
mutate(
image = map(meme_name, meme_get),
path = paste0(getwd(), "/", meme_name, ".jpg")
) %>%
@matt-dray
matt-dray / googleVis_test.Rmd
Last active April 23, 2019 11:15
Display a {googleVis} graphic in R Markdown
---
title: "googleVis test"
author: "Matt Dray"
date: "23/04/2019"
output: html_document
---
Set up example dataframe from [the {googleVis} examples](https://cran.r-project.org/web/packages/googleVis/vignettes/googleVis_examples.html) CRAN vignette.
```{r}
@matt-dray
matt-dray / sankey-nodes-time.R
Created May 30, 2019 11:17
A Sankey chart using {networkD3} showing flow between nodes at three time points
# Inspired by https://www.displayr.com/sankey-diagrams-r/
library(networkD3)
# Two dataframes are needed: one with nodes, one with links
nodes <- data.frame(
"name" = c(
# Nodes at time = 1
"Node A t1", # Node 0
@matt-dray
matt-dray / brickr_soccer_kit.R
Created May 30, 2019 20:56
A function to create a soccer player using {brickr}
# Packages
library(brickr)
library(rayshader)
library(tibble)
# Function that takes colours for different parts of the player model.
# Colour codes as per brickr::lego_colors
# Output is a bricks_from_table() list object
# Output should be fed to display_bricks()
@matt-dray
matt-dray / brickr-soccer-colours.R
Created June 1, 2019 15:40
Colour combinations for certain soccer teams to be used to create 3D {brickr} models
# Create a soccer player using {brickr}
# This file specifies some colour combinations for certain teams
# Blog post: https://www.rostrum.blog/2019/05/31/brickr-soccer/
# You need to load some packages
# library(brickr)
# library(dray) # from remotes::install_github("matt-dray/dray")
# After creating a colour specification, pass it to
# bricks_from_table() %>% display_bricks() to render it
@matt-dray
matt-dray / scrape_list_dataframe.py
Last active December 21, 2021 20:51
Check URL is responsive and add tuples of information about it to an empty list, then turn to DataFrame and save
# Demo URLs
urls = ['https://www.google.com', 'https://en.wikipedia.org', 'https://www.asadfxzcvgjkerty.com', 'https://www.rostrum.blog']
# Set up empty list to fill with content from the for loop
output = []
# Begin for loop
for url in urls:
try:
@matt-dray
matt-dray / get_expansion.R
Last active September 5, 2019 14:41
Expand c(1:3) and c(1, 2, 3) to c(1, 2, 2, 3, 3, 3) and c(1, 1, 2, 1, 2, 3) ready for purrr::map() functions in R
# This isn't optimal and there's probably
# a function that does all this already
# Packages
library(dplyr)
library(purrr)
library(tibble)
# Write function
get_expansion <- function(x, y) {
@matt-dray
matt-dray / expand-details-date-sessioninfo.Rmd
Last active October 22, 2019 08:07
Handy snippets for rostrum.blog
---
<details><summary>Session info</summary>
```{r sessioninfo, echo=FALSE}
paste("Last updated", Sys.Date())
xfun::session_info()
```
</details>