Skip to content

Instantly share code, notes, and snippets.

@georgeblck
Created December 11, 2024 14:51
Show Gist options
  • Save georgeblck/9e138ee19de9091c625e279cf671d2d0 to your computer and use it in GitHub Desktop.
Save georgeblck/9e138ee19de9091c625e279cf671d2d0 to your computer and use it in GitHub Desktop.
Create custom A4 sized progress tracker grid with numbering
# Load the grid package
library(grid)
# Function to create a tightly packed grid of squares with numbering
create_numbered_square_pdf <- function(file, n_squares = 600, n_cols = 30) {
# Calculate the number of rows needed
n_rows <- ceiling(n_squares / n_cols)
# Define the page size (A4 in inches)
page_width <- 8.27 # A4 width in inches
page_height <- 11.69 # A4 height in inches
# Calculate the size of each square
square_width <- page_width / n_cols
square_height <- page_height / n_rows
# Open a PDF device
pdf(file, width = page_width, height = page_height) # A4 size
# Draw the squares row by row
for (row in 1:n_rows) {
for (col in 1:n_cols) {
# Calculate the index of the current square
index <- (row - 1) * n_cols + col
if (index > n_squares) break
# Calculate the square position
x_center <- (col - 1) * square_width + square_width / 2
y_center <- page_height - (row - 1) * square_height - square_height / 2
# Draw the square
grid.rect(
x = unit(x_center, "in"),
y = unit(y_center, "in"),
width = unit(square_width, "in"),
height = unit(square_height, "in"),
gp = gpar(col = "black", fill = NA)
)
# Add the number inside the square
grid.text(
label = index,
x = unit(x_center, "in"),
y = unit(y_center, "in"),
gp = gpar(fontsize = 6, col = "black")
)
}
}
# Close the PDF device
dev.off()
}
# Call the function
create_numbered_square_pdf("numbered_squares_grid.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment