Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

gt::tab_row_group() can't find row names

I’m trying to use the gt::tab_row_group function to subset a table by row name, but the tutorial I’m following from the vignettes/gt.Rmd doesn’t seem to work (very possible I’m doing something wrong). The error message I keep getting:

Error in `tab_row_group()`:
! Rows `Australia` and `Greenland` do not exist in the data.

I don’t have any other packages loaded. I’ve downloaded the development version of gt() via Github and tried interpreting the backtrace log to see where things go wrong, but I’m not understanding the problem.

library(dplyr)
library(gt)

install.packages("devtools")

devtools::install_github("rstudio/gt")
gt::tab_row_group

Using the data islands from R

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

islands_tbl <- 
  tibble(
    name = names(islands),
    size = islands
  ) |>
  arrange(desc(size)) |>
  slice(1:10)

# Create a gt Table
gt_tbl <- gt(islands_tbl)
    
  # Show the gt Table
  gt_tbl |> 
  tab_row_group(
    label = "continent",
    rows = 1:6
  ) |>
  tab_row_group(
    label = "country",
    rows = c("Australia", "Greenland")
  ) |>
  tab_row_group(
    label = "subregion",
    rows = c("New Guinea", "Borneo")
  )

Error in `tab_row_group()`:
! Rows `Australia` and `Greenland` do not exist in the data.
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/rlang_error>
Error in `tab_row_group()`:
! Rows `Australia` and `Greenland` do not exist in the data.
---
Backtrace:
    ▆
 1. ├─gt::tab_row_group(...)
 2. │ └─gt:::stop_if_not_gt_tbl(data = data)
 3. │   └─gt:::is_gt_tbl(data = data)
 4. └─gt::tab_row_group(...)
Run rlang::last_trace(drop = FALSE) to see 6 hidden frames.
> rlang::last_trace(drop = FALSE)
<error/rlang_error>
Error in `tab_row_group()`:
! Rows `Australia` and `Greenland` do not exist in the data.
---
Backtrace:
     ▆
  1. ├─gt::tab_row_group(...)
  2. │ └─gt:::stop_if_not_gt_tbl(data = data)
  3. │   └─gt:::is_gt_tbl(data = data)
  4. └─gt::tab_row_group(...)
  5.   └─gt:::resolve_rows_i(expr = !!row_expr, data = data)
  6.     └─gt:::resolve_rows_l(...)
  7.       └─gt:::normalize_resolved(...)
  8.         └─gt:::resolver_stop_on_character(...)
  9.           └─cli::cli_abort(...)
 10.             └─rlang::abort(...)

The code I’m using is copy-pasted from…Source

Here is my intended result:

Table organized by row name

>Solution :

The issue is that gt has to know where to look for the categories, i.e. you have to provide a column name (for the first tab_row_group this is not necessary as your provide the row indices).

A first option would be to to provide the column name using rowname_col=:

library(gt)

gt(islands_tbl, rowname_col = "name") |> 
  tab_row_group(
    label = "continent",
    rows = 1:6
  ) |>
  tab_row_group(
    label = "country",
    rows = c("Australia", "Greenland")
  ) |>
  tab_row_group(
    label = "subregion",
    rows = c("New Guinea", "Borneo")
  )

enter image description here

Or as a second option include the column name in the selecting expression, i.e. use name %in% ...:

gt(islands_tbl) |>
  tab_row_group(
    label = "continent",
    rows = 1:6
  ) |>
  tab_row_group(
    label = "country",
    rows = name %in% c("Australia", "Greenland")
  ) |>
  tab_row_group(
    label = "subregion",
    rows = name %in% c("New Guinea", "Borneo")
  )

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading