Skip to content

Commit

Permalink
efficient tabularray
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Nov 3, 2024
1 parent e096d62 commit 82748f6
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ setMethod(
sty$alignv[which(sty$alignv == "t")] <- "h"
sty$alignv[which(sty$alignv == "m")] <- "m"

for (spec in stats::na.omit(sty$tabularray_inner)) {
x@table_string <- tabularray_insert(x@table_string, content = spec, type = "inner")
}

for (spec in stats::na.omit(sty$tabularray_outer)) {
x@table_string <- tabularray_insert(x@table_string, content = spec, type = "inner")
}

set <- span <- rep("", nrow(rec))

for (row in seq_len(nrow(sty))) {
Expand Down Expand Up @@ -115,11 +123,10 @@ setMethod(
}

clean <- function(k) {
k <- gsub("^\\s*,", "", k)
k <- gsub(",\\s*,", ",,", k)
k <- gsub("\\s+", " ", k)
k <- gsub("\\s*", "", k)
k <- gsub(",+", ",", k)
k <- gsub("^[,|\\s]*", "", k, perl = TRUE)
k <- gsub("^,", "", k, perl = TRUE)
k <- gsub(",", ", ", k)
k <- trimws(k)
return(k)
}
Expand All @@ -131,6 +138,7 @@ setMethod(

# complete columns
all_i <- seq_len(x@nrow + x@nhead)
browser()
rec <- do.call(rbind, by(rec, list(rec$j, rec$set, rec$span), function(k) {
transform(k, complete_column = all(all_i %in% k$i))
}))
Expand All @@ -139,32 +147,32 @@ setMethod(
for (s in spec) {
x@table_string <- tabularray_insert(x@table_string, content = s, type = "inner")
}
rec <- rec[!rec$complete_column,, drop = FALSE]
rec <- rec[!rec$complete_column, , drop = FALSE]

# complete rows
all_j <- seq_len(x@ncol)
rec <- do.call(rbind, by(rec, list(rec$i, rec$set, rec$span), function(k) {
transform(k, complete_row = all(all_j %in% k$j))
}))
rows <- unique(rec[rec$complete_row, colnames(rec) != "j"])
spec <- sprintf("row{%s}={%s}{%s}", rows$i, rows$span, rows$set)
for (s in spec) {
x@table_string <- tabularray_insert(x@table_string, content = s, type = "inner")
if (nrow(rec) > 0) {
all_j <- seq_len(x@ncol)
rec <- do.call(rbind, by(rec, list(rec$i, rec$set, rec$span), function(k) {
transform(k, complete_row = all(all_j %in% k$j))
}))
rows <- unique(rec[rec$complete_row, colnames(rec) != "j"])
spec <- sprintf("row{%s}={%s}{%s}", rows$i, rows$span, rows$set)
for (s in spec) {
x@table_string <- tabularray_insert(x@table_string, content = s, type = "inner")
}
rec <- rec[!rec$complete_row,, drop = FALSE]
}
rec <- rec[!rec$complete_row,, drop = FALSE]

# cells
spec <- sprintf("cell{%s}{%s}={%s}{%s}", rec$i, rec$j, rec$span, rec$set)
for (s in spec) {
x@table_string <- tabularray_insert(x@table_string, content = s, type = "inner")
if (nrow(rec) > 0) {
spec <- sprintf("cell{%s}{%s}={%s}{%s}", rec$i, rec$j, rec$span, rec$set)
for (s in spec) {
x@table_string <- tabularray_insert(x@table_string, content = s, type = "inner")
}
}

for (spec in stats::na.omit(sty$tabularray_inner)) {
x@table_string <- tabularray_insert(x@table_string, content = spec, type = "inner")
}
for (spec in stats::na.omit(sty$tabularray_outer)) {
x@table_string <- tabularray_insert(x@table_string, content = spec, type = "inner")
}
# horizontal lines


return(x)

Expand Down

0 comments on commit 82748f6

Please sign in to comment.