Skip to content

Instantly share code, notes, and snippets.

@rasmusab
Created March 12, 2023 23:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasmusab/e593917198a59d2824ca06d836f46f93 to your computer and use it in GitHub Desktop.
Save rasmusab/e593917198a59d2824ca06d836f46f93 to your computer and use it in GitHub Desktop.
A couple of quick hacks that can be done with a scraped IBA cocktails dataset
# This script takes the scraped IBA cocktails dataset found here:
# https://github.com/rasmusab/iba-cocktails
# And produces a four page PDF with all the IBA cocktails and animated GIFs that
# loops through these, as well.
library(tidyverse)
library(glue)
library(ggtext)
library(gridExtra)
cocktails_url = "https://raw.githubusercontent.com/rasmusab/iba-cocktails/9148d3302f582b06695684f3bb446631ab99d160/wikipedia/iba-cocktails-wiki.csv"
cocktails <- read_csv(cocktails_url)
a4_width <- 21.0 # cm
a4_height <- 29.7 # cm
n_drinks_wide <- 4
n_drinks_high <- 6
drink_margin <- 0.1 # cm
drink_width <- a4_width / n_drinks_wide - drink_margin * 2
drink_height <- a4_height / n_drinks_high - drink_margin * 2
text_plots <- cocktails |>
mutate(
ingredients_text = ingredients |>
str_split(',') |>
map_chr(~ glue_collapse(glue("- {.x}"), sep = "<br>")),
cocktail_text = glue(.sep = "<br>",
"<b>{name}</b>",
"{ingredients_text}",
"{method}"
)
) |>
rowwise() |>
mutate(text_plot = list(
ggplot(tibble(cocktail_text = cocktail_text), aes(label = cocktail_text)) +
theme_void() +
labs(x = NULL, y = NULL) +
scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
geom_textbox(
x = 0.5, y = 0.5, size = 2.35,
width = unit(drink_width, "cm"),
height = unit(drink_height, "cm"),
box.margin = unit(c(0.05, 0.05, 0.05, 0.05), "cm")
)
)) |>
pull(text_plot)
ggsave("iba-cocktails.pdf",
plot = marrangeGrob(text_plots, ncol=n_drinks_wide, nrow=n_drinks_high, top = NULL),
width = a4_width, height = a4_height, units = "cm"
)
# Now making animated GIF version of the above, one fast and one slow
library(gifski)
# scaling and res found by trial and error
scaling = 100
res = 250
save_gif(
walk(text_plots, print), gif_file = "iba-cocktails-fast.gif", delay = 0.1,
width = drink_width * scaling, heigh = drink_height * scaling, res = res
)
save_gif(
walk(text_plots, print), gif_file = "iba-cocktails-slow.gif", delay = 2,
width = drink_width * scaling, heigh = drink_height * scaling, res = res
)
# Now making a searchable datatable
library(DT)
cocktails_dt <- cocktails |>
select(name, ingredients, method) |>
mutate(ingredients = ingredients |>
str_split(",") |>
map_chr(~ paste0("- ", .x, collapse = "<br>"))
) |>
datatable(escape = FALSE)
saveWidget(cocktails_dt, "cocktails-table.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment