Can't figure out problem in gt table.

I keep getting this error, when working on an example gt table with Palmer penguins

Error in resolver_stop_on_character():
! The following row(s) do not exist in the data: Gentoo.
Run rlang::last_error() to see where the error occurred.

I run into the problem on the last line of code for the tab_footnote(). Everything else works fine before it. This is from an example on YouTube - Rich Iannone || Making Beautiful Tables with {gt} || RStudio - YouTube

Here's my code :

penguins %>%
  group_by(species) %>%
  summarize_at(
    .vars = c(
      "bill_length_mm",
      "bill_depth_mm",
      "flipper_length_mm",
      "body_mass_g"),
    .funs = ~ mean(., na.rm = TRUE)) %>%
  gt(rowname_col = "species") %>%
  tab_header(
    title = md("Summary of the `penguins` dataset"),
    subtitle = md("**Three** years of data on penguins on *three* islands")
  ) %>%
  cols_label(
    bill_length_mm = md("Bill Length,<br>mm"),
    bill_depth_mm = md("Bill Depth,<br>mm"),
    flipper_length_mm = md("Flipper Length, <br>mm"),
    body_mass_g = md("Body Mass, <br>kg")
  ) %>%
  opt_align_table_header(align = "left") %>%
  fmt_number(columns = everything()) %>%
  fmt_number(columns = body_mass_g, scale_by = 1/1000) %>%
  cols_width(
    bill_length_mm ~ px(120),
    bill_depth_mm ~ px(120),
    flipper_length_mm ~ px(120),
    body_mass_g ~ px(120), 
    everything() ~ px(100)) %>%
  tab_source_note(source_note = md("Data provided 
        by the *Palmer Penguins* Data Package")) %>% 
  tab_footnote(
    footnote = "The largest penguin ever", 
    locations = cells_stub(rows = "Gentoo"))

It does seem like a bug that should be raised on the gt github issues page; but in the meantime you can achieve the end result you desire by changing
rows = "Gentoo" to rows = 3

1 Like

Hi @jvanster,

This has to do with the species column being a factor, so what's actually being converted to a rowname is the numeric representation of the factor level. You can verify this by doing cells_stub(rows = "3").

This is something that should be brought to the developers' attention if it isn't already a issue on Issues · rstudio/gt · GitHub .

But in the meantime you can convert the values in the species column to characters before piping it into gt() and the rest of your code will work fine.

penguins %>%
  group_by(species) %>%
  summarize_at(
    .vars = c(
      "bill_length_mm",
      "bill_depth_mm",
      "flipper_length_mm",
      "body_mass_g"),
    .funs = ~ mean(., na.rm = TRUE)) %>%
  mutate(species = as.character(species)) %>% # Basically, add this line to your code
  gt(rowname_col = "species") %>%
  ...
  ...

All the best,
Hlynur

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.