Skip to content

Commit

Permalink
Issue #99: Do not indent header or footer in group_tt(i)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 24, 2024
1 parent d0a2d1f commit 7e96c65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# News

## Development

## 0.0.1

Initial package release. Yay!
6 changes: 5 additions & 1 deletion R/group_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ group_bootstrap_row <- function(x, i, j, indent = 1, ...) {
idx <- insert_values(seq_len(m$nrows), rep(NA, length(i)), i)
idx_old <- idx$new[!is.na(idx$old)]
idx_new <- idx$new[is.na(idx$old)]
# cannot be style_tt, because we already have a string bootstrap table at this stage

# limit index ot number of rows to avoid styling header or footer
idx_old <- idx_old[idx_old <= meta(x)$nrows]

# should not be style_tt, because we already have a string bootstrap table at this stage
out <- style_bootstrap(out, i = idx_old, j = 1, indent = indent)

# if there's a two-level header column multi-span, we want it centered.
Expand Down
44 changes: 22 additions & 22 deletions R/group_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ group_tabularray <- function(x, i, j, indent) {
}
if (!is.null(i)) {
out <- group_tabularray_row(out, i, indent)
}
}
return(out)
}


group_tabularray_col <- function(x, j) {

m <- meta(x)

out <- strsplit(x, split = "\\n")[[1]]
Expand All @@ -32,10 +31,11 @@ group_tabularray_col <- function(x, j) {
grep("\\toprule", out, fixed = TRUE)
))

out <- c(out[1:idx],
# empty lines can break latex
trimws(header),
out[(idx + 1):length(out)])
out <- c(
out[1:idx],
# empty lines can break latex
trimws(header),
out[(idx + 1):length(out)])
out <- paste(out, collapse = "\n")

# rebuild including meta before style_tt
Expand All @@ -44,24 +44,23 @@ group_tabularray_col <- function(x, j) {

for (k in seq_along(j)) {
z <- min(j[[k]])
args <- list(tt_build_now = TRUE,
x = out,
# the new header is always first row and
# style_tt always adds nhead to index
i = 1 - meta(out)$nhead,
j = z,
align = "c",
colspan = max(j[[k]]) - min(j[[k]]) + 1)
args <- list(
tt_build_now = TRUE,
x = out,
# the new header is always first row and
# style_tt always adds nhead to index
i = 1 - meta(out)$nhead,
j = z,
align = "c",
colspan = max(j[[k]]) - min(j[[k]]) + 1)
out <- do.call(style_tt, args)
}

return(out)

}


group_tabularray_row <- function(x, i, indent) {

m <- meta(x)

if (is.null(names(i))) {
Expand Down Expand Up @@ -92,7 +91,8 @@ group_tabularray_row <- function(x, i, indent) {
attr(tab, "tinytable_meta") <- m
class(tab) <- class(x)

cellspec <- sprintf("cell{%s}{%s}={%s}{%s},",
cellspec <- sprintf(
"cell{%s}{%s}={%s}{%s},",
idx$new[is.na(idx$old)] + m$nhead,
1,
paste0("c=", m$ncols),
Expand All @@ -102,8 +102,8 @@ group_tabularray_row <- function(x, i, indent) {
tab <- tabularray_insert(tab, content = cellspec, type = "inner")

# we also want to indent the header
i <- idx$new[!is.na(idx$old)] + m$nhead
if (m$nhead > 0) i <- c(1:m$nhead, i)
i <- idx$new[!is.na(idx$old)] + m$nhead
# if (m$nhead > 0) i <- c(1:m$nhead, i)
cellspec <- sprintf("cell{%s}{%s}={%s},\n", i, 1, sprintf("preto={\\hspace{%sem}}", indent))
cellspec <- paste(cellspec, collapse = "")
tab <- tabularray_insert(tab, content = cellspec, type = "inner")
Expand All @@ -117,12 +117,12 @@ insert_values <- function(vec, values, positions) {
if (length(values) != length(positions)) {
stop("The length of values and positions must be the same")
}

# Sort the positions in decreasing order along with their corresponding values
ord <- order(positions, decreasing = TRUE)
values <- values[ord]
positions <- positions[ord]

# Create a vector of indices for the original vector
original_indices <- 1:length(vec)

Expand All @@ -132,7 +132,7 @@ insert_values <- function(vec, values, positions) {
vec <- append(vec, values[i], after = positions[i] - 1)
original_indices <- append(original_indices, NA, after = positions[i] - 1)
}

# Return the extended vector and the original indices vector
return(data.frame(vec = vec, old = original_indices, new = seq_along(vec)))
}
Expand Down

0 comments on commit 7e96c65

Please sign in to comment.