gt icon indicating copy to clipboard operation
gt copied to clipboard

`gt_split()` with group/row labels

Open bzkrouse opened this issue 2 years ago • 2 comments

Prework

Description

Unable to use gt_split() on tables with row groups or row labels.

Reproducible example

  • [x] Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • [x] Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • [x] Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • [x] Readable: format your code according to the tidyverse style guide.
  1. Trying to split a table with row groups:
gtcars |>
  dplyr::slice_head(n = 10) |>
  dplyr::select(mfr, model, year, msrp) |>
  gt(groupname_col = "mfr") |>
  gt_split(row_every_n = 5)

produces the error:

Error in !is.null(group_id) && !has_two_col_stub && group_row_start ==  : 
  'length = 2' in coercion to 'logical(1)'
  1. Trying to split a table with row labels:
gtcars |>
  dplyr::slice_head(n = 10) |>
  dplyr::select(mfr, model, year, msrp) |>
  gt(rowname_col= "mfr") |>
  gt_split(row_every_n = 5)

produces the error:

Error in `table_body[[stub_var]]`:
! Can't extract column with `stub_var`.
✖ Subscript `stub_var` must be a location, not a character `NA`.

Expected result

I would have expected the table to split into 2 tables (would it be based on 5 rows of the body cells?), while retaining the group/row labels.

bzkrouse avatar Jun 23 '23 12:06 bzkrouse

Very glad you caught this, thanks! Will fix this one soon.

rich-iannone avatar Jun 23 '23 17:06 rich-iannone

I have a similar problem when having tables with merged cols:

Doesn't function properly with both cols_merge and gt_split; however, either of them individually works.

gtcars |>
  dplyr::slice_head(n = 10) |>
  dplyr::select(mfr, model, year, msrp) |>
  gt() |>
  cols_merge(
    columns = c("mfr","model"),
    pattern = "{1} ({2})"
  ) %>%
  gt_split(col_slice_at = "year")
Error in `data_tbl[rows, columns]`:
! Can't subset columns that don't exist.
✖ Column `model` doesn't exist.

zachariae avatar Apr 16 '24 10:04 zachariae