gt icon indicating copy to clipboard operation
gt copied to clipboard

Facilitate direct use of matrix

Open gvwilson opened this issue 5 years ago • 1 comments

I would to be able to use gt to format a grid of text, which is represented as a matrix:

  1. I need to place names randomly on a 10x10 grid and create a pretty printable PDF.
  2. The most natural way to place the names is to create a 10x10 matrix of empty strings, then overwrite the empty strings in some locations with names. (I'm using a matrix rather than a tibble because I don't think of the columns as being meaningful---they're just locations.)

Code to date is included below; it assumes a CSV file with one name per line.

# Grid size.
SIZE <- 10
SIZE_2 <- SIZE * SIZE

# Command-line arguments.
args <- commandArgs(trailingOnly = TRUE)
filename <- args[[1]]
seed <- args[[2]]

# Read names.
names <- read_csv(filename)$Name

# Determine XY positions (0-based to make subsequent math easier).
set.seed(seed)
shuffle <- sample(0:(SIZE_2 - 1))

# Fill matrix (converting back to 1-based indices).
table <- matrix(rep("", SIZE_2), SIZE, SIZE)
for (i in 1:length(names)) {
    x <- (shuffle[i] %% SIZE) + 1
    y <- (shuffle[i] %/% SIZE) + 1
    table[x, y] <- names[i]
}
print(table)

gvwilson avatar Feb 01 '19 18:02 gvwilson

This seems like a great idea, thanks for the suggestion! Once work begins on this, I will likely follow up in this issue with design-related questions.

rich-iannone avatar Feb 01 '19 18:02 rich-iannone