Skip to content

Instantly share code, notes, and snippets.

@jnolis
Last active August 31, 2023 13:17
Show Gist options
  • Save jnolis/601062e7967c3865b758a907d7ee1bfd to your computer and use it in GitHub Desktop.
Save jnolis/601062e7967c3865b758a907d7ee1bfd to your computer and use it in GitHub Desktop.
Generate bingo sheets
# This code generates however many bingo sheets you need
# It requires you to have "squares.csv", a single column csv file with
# name "square" and a row for the text in each square.
#
# You also need to install wkhtmltopdf https://wkhtmltopdf.org/ and
# add it to your system path. This converts the html to a pdf page
# libraries: tidyverse, glue, rmarkdown
#
# Also make a folder "output" to store them to
library(tidyverse)
n_boards <- 100 # number of boards to generate
squares_raw <- read_csv("squares.csv")$square
n_squares <- length(squares_raw)
col_assignment <- sample(rep(1:5,ceiling(n_squares/5))[1:n_squares])
squares <- map(1:5,~squares_raw[.x == col_assignment])
generate_board <- function(squares){
board <- map(1:5, ~sample(squares[[.x]])[1:5])
board[[3]][3] <- "FREE\nSPACE"
board |>
set_names(c("B","I","N","G","O")) |>
as_tibble()
}
print_board <- function(n, total){
board <- generate_board(squares)
rmarkdown::render("board.Rmd",output_file = "temp.html", params=list(board=board, n=n, total=total))
system(glue::glue("wkhtmltopdf --javascript-delay 1 temp.html boards/board_{str_pad(n,width=5,pad='0')}.pdf"))
}
map(1:n_boards, print_board, total=n_boards)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment