library(gt) library(dplyr) random_string <- function(length) { char_set <- c(letters, LETTERS, 0:9) paste0(sample(char_set, length, replace = TRUE), collapse = "") } for (i in 1:100) { num_rows <- 100 num_cols <- 4 df <- data.frame(ID = replicate(num_rows, random_string(6)), matrix(paste0("C", rep(1:(num_cols-1), each = num_rows), sample(2:4, num_rows*(num_cols-1), replace = TRUE)), ncol = num_cols-1), stringsAsFactors = FALSE) colnames(df)[-1] <- paste0("D", 1:(num_cols-1)) filename <- sprintf("P%03d.csv", i) write.csv(df, filename, row.names = FALSE, quote = FALSE) } P004 <- read.csv("P004.csv") P004table = gt(P004) P004table <- P004table %>% tab_row_group( rows = which(rownames(P004) %in% rownames(P004[P004$D1 == "C12" & P004$D2 == "C22" & P004$D3 == "C32",])), label = "C12, C22, C32" ) %>% tab_row_group( rows = which(rownames(P004) %in% rownames(P004[P004$D1 == "C13" & P004$D2 == "C23" & P004$D3 == "C33",])), label = "C13, C23, C33" ) %>% tab_row_group( rows = which(rownames(P004) %in% rownames(P004[P004$D1 == "C14" & P004$D2 == "C24" & P004$D3 == "C34",])), label = "C14, C24, C34" ) %>% tab_row_group( rows = which(rownames(P004) %in% rownames(P004[P004$D1 == "C12" & P004$D2 == "C23" & P004$D3 == "C32",])), label = "C12, C23, C32" ) %>% tab_row_group( rows = which(rownames(P004) %in% rownames(P004[P004$D1 == "C13" & P004$D2 == "C24" & P004$D3 == "C34",])), label = "C13, C24, C34" ) %>% tab_row_group( rows = which(rownames(P004) %in% rownames(P004[P004$D1 == "C13" & P004$D2 == "C22" & P004$D3 == "C34", ])), label = "C13, C22, C34" ) P004table <- P004table %>% tab_style( style = list( cell_fill(color = "#FFCC00") ), locations = cells_row_groups() ) gtsave(P004table, file = "P004_table.html") system("wkhtmltopdf P004_table.html table.pdf")