Skip to content

Native grouping API #360

@ggrothendieck

Description

@ggrothendieck

I have been using huxtable recently and will report a few points where the code might have been shorter if the feature existed.

Currently, inserting a row shifts the index of every row below it, forcing us to use cumulative sums and index math.

# We have to calculate the 'pos' for every single insertion.
df <- data.frame(Asset = c("A","B","C", "D","E","F"), Val = 1:6)
ht <- as_hux(df)

# We must calculate offsets manually
group_sizes <- c(3, 3)
insert_at <- c(1, 4) # Original indices

for (i in seq_along(insert_at)) {
  # (i-1) accounts for rows already added earlier in the loop
  current_pos <- insert_at[i] + (i - 1) 
  ht <- insert_row(ht, c("Group Header", ""), after = current_pos)
  ht <- set_colspan(ht, current_pos + 1, 1, 2)
}

What would be nice would be to shorten this to one line

# One command handles the splitting and header insertion automatically.
ht %>% add_group_headers(by = "Sector", style = list(bold = TRUE))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions