############################################
# FULL SCRIPT — data, super_data, selections, gt tables
############################################
# install.packages("gt") # uncomment if needed
library(gt)
# ------------------------------------------
# 1) DATA: 100 kinds (order of A,B,C,D defines grouping)
# ------------------------------------------
data <- list(
# LION (A,B,C,D)
# list(kind="k001", ref="000001", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP3.5"),
# list(kind="k002", ref="000002", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP5"),
# list(kind="k003", ref="000003", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP6"),
# list(kind="k004", ref="000004", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP10"),
# list(kind="k005", ref="000005", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP11"),
# list(kind="k006", ref="000006", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP13.5"),
# list(kind="k007", ref="000007", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP15"),
# list(kind="k008", ref="000008", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP16"),
# list(kind="k009", ref="000009", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP20"),
# list(kind="k010", ref="000010", A="%@#$", B="!$%*", Manufacturer="Arrow Valves", Model="DP25"),
# EAGLE (B,A,C,D)
list(kind="k011", ref="000011", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k012", ref="000012", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k013", ref="000013", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k014", ref="000014", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k015", ref="000015", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k016", ref="000016", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k017", ref="000017", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k018", ref="000018", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k019", ref="000019", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
list(kind="k020", ref="000020", B="!$%*", A="%@#$", C="&^#@", D="~!?*"),
# WOLF (C,A,D,B)
list(kind="k021", ref="000021", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k022", ref="000022", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k023", ref="000023", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k024", ref="000024", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k025", ref="000025", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k026", ref="000026", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k027", ref="000027", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k028", ref="000028", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k029", ref="000029", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
list(kind="k030", ref="000030", C="&^#@", A="%@#$", D="~!?*", B="!$%*"),
# TIGER (D,A,B,C)
list(kind="k031", ref="000031", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k032", ref="000032", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k033", ref="000033", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k034", ref="000034", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k035", ref="000035", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k036", ref="000036", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k037", ref="000037", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k038", ref="000038", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k039", ref="000039", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
list(kind="k040", ref="000040", D="~!?*", A="%@#$", B="!$%*", C="&^#@"),
# BEAR (A,C,B,D)
list(kind="k041", ref="000041", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k042", ref="000042", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k043", ref="000043", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k044", ref="000044", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k045", ref="000045", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k046", ref="000046", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k047", ref="000047", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k048", ref="000048", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k049", ref="000049", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
list(kind="k050", ref="000050", A="%@#$", C="&^#@", B="!$%*", D="~!?*"),
# FALCON (B,D,A,C)
list(kind="k051", ref="000051", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k052", ref="000052", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k053", ref="000053", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k054", ref="000054", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k055", ref="000055", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k056", ref="000056", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k057", ref="000057", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k058", ref="000058", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k059", ref="000059", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
list(kind="k060", ref="000060", B="!$%*", D="~!?*", A="%@#$", C="&^#@"),
# SHARK (C,D,A,B)
list(kind="k061", ref="000061", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k062", ref="000062", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k063", ref="000063", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k064", ref="000064", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k065", ref="000065", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k066", ref="000066", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k067", ref="000067", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k068", ref="000068", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k069", ref="000069", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
list(kind="k070", ref="000070", C="&^#@", D="~!?*", A="%@#$", B="!$%*"),
# OWL (A,B,D,C)
list(kind="k071", ref="000071", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k072", ref="000072", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k073", ref="000073", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k074", ref="000074", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k075", ref="000075", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k076", ref="000076", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k077", ref="000077", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k078", ref="000078", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k079", ref="000079", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
list(kind="k080", ref="000080", A="%@#$", B="!$%*", D="~!?*", C="&^#@"),
# RABBIT (D,B,C,A)
list(kind="k081", ref="000081", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k082", ref="000082", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k083", ref="000083", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k084", ref="000084", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k085", ref="000085", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k086", ref="000086", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k087", ref="000087", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k088", ref="000088", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k089", ref="000089", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
list(kind="k090", ref="000090", D="~!?*", B="!$%*", C="&^#@", A="%@#$"),
# PANTHER (B,A,D,C)
list(kind="k091", ref="000091", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k092", ref="000092", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k093", ref="000093", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k094", ref="000094", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k095", ref="000095", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k096", ref="000096", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k097", ref="000097", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k098", ref="000098", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k099", ref="000099", B="!$%*", A="%@#$", D="~!?*", C="&^#@"),
list(kind="k100", ref="000100", B="!$%*", A="%@#$", D="~!?*", C="&^#@")
)
# ------------------------------------------
# 2) SUPER_DATA: explicit mapping (no sprintf/paste)
# ------------------------------------------
super_data <- data.frame(
entry = I(list(
list(superkind = "lion", kinds = c("k001","k002","k003","k004","k005","k006","k007","k008","k009","k010")),
list(superkind = "eagle", kinds = c("k011","k012","k013","k014","k015","k016","k017","k018","k019","k020")),
list(superkind = "wolf", kinds = c("k021","k022","k023","k024","k025","k026","k027","k028","k029","k030")),
list(superkind = "tiger", kinds = c("k031","k032","k033","k034","k035","k036","k037","k038","k039","k040")),
list(superkind = "bear", kinds = c("k041","k042","k043","k044","k045","k046","k047","k048","k049","k050")),
list(superkind = "falcon", kinds = c("k051","k052","k053","k054","k055","k056","k057","k058","k059","k060")),
list(superkind = "shark", kinds = c("k061","k062","k063","k064","k065","k066","k067","k068","k069","k070")),
list(superkind = "owl", kinds = c("k071","k072","k073","k074","k075","k076","k077","k078","k079","k080")),
list(superkind = "rabbit", kinds = c("k081","k082","k083","k084","k085","k086","k087","k088","k089","k090")),
list(superkind = "panther", kinds = c("k091","k092","k093","k094","k095","k096","k097","k098","k099","k100"))
))
)
# ------------------------------------------
# 3) SELECTIONS: list of selections (ref-kind pairs)
# (Adjust or extend these as you like; must use k001..k100)
# ------------------------------------------
selections <- list(
list(ref="583920", kind="k001"),
list(ref="104857", kind="k006"),
list(ref="749302", kind="k012"),
list(ref="658411", kind="k019"),
list(ref="920374", kind="k021"),
list(ref="837465", kind="k027"),
list(ref="194028", kind="k034"),
list(ref="305671", kind="k040"),
list(ref="874219", kind="k041"),
list(ref="583047", kind="k046"),
list(ref="219740", kind="k052"),
list(ref="468302", kind="k059"),
list(ref="795180", kind="k061"),
list(ref="903712", kind="k067"),
list(ref="127654", kind="k071"),
list(ref="508329", kind="k079"),
list(ref="641780", kind="k082"),
list(ref="734965", kind="k091"),
list(ref="210398", kind="k097"),
list(ref="876540", kind="k100")
)
# ------------------------------------------
# 4) Build gt table for each superkind:
# rows = selections matching that superkind; columns = ref, kind, A,B,C,D
# ------------------------------------------
# kind -> object lookup
data_by_kind <- setNames(data, vapply(data, function(x) x$kind, ""))
# selections -> data.frame with A,B,C,D resolved
sel_df <- do.call(
rbind,
lapply(selections, function(s) {
obj <- data_by_kind[[s$kind]]
data.frame(
ref = s$ref,
kind = s$kind,
A = obj$A, B = obj$B, C = obj$C, D = obj$D,
stringsAsFactors = FALSE
)
})
)
# superkind -> vector of kinds (explicit, hard match)
super_map <- setNames(
lapply(super_data$entry, function(e) e$kinds),
vapply(super_data$entry, function(e) e$superkind, "")
)
# Create a gt table per superkind (only rows with selections)
gt_tables <- lapply(names(super_map), function(sk) {
rows <- sel_df[ sel_df$kind %in% super_map[[sk]], c("ref","A","B","C","D") ]
gt(rows) |>
tab_header(title = paste0("Superkind: ", sk)) |>
cols_label(
ref = "ref",
A = "A",
B = "B",
C = "C",
D = "D"
)
})
names(gt_tables) <- names(super_map)
# Usage examples:
# gt_tables[["lion"]]
# gt_tables[["eagle"]]
# gt_tables[["panther"]]
# Combine all gt tables into one HTML string
combined_html <- paste(
lapply(gt_tables, function(tbl) as_raw_html(tbl)),
collapse = "