Skip to content

Option to omit groupname_cols with NA values #1215

Closed

Description

@alex-lauer

Prework

  • [ x ] Read and agree to the code of conduct and contributing guidelines.
  • [ x ] If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
  • [ x ] For any problems you identify, a minimal reproducible example so the maintainer can troubleshoot. 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.

Question

Hi @rich-iannone,

I created a couple of pharma tables lately for the rtrs-wg tables book and had the following idea: I often have to use dplyr::add_rows() to create first row stating something like Patients with at least in a column that would end up being a rowname_col, while the groupname_col would remain empty.

I attached an example code below and would like to ask, if there could be an option in gt() to omit the creating of groupname columns whenever the specified column contains NA's and create groups for the non-NA group values.

library(tidyverse)
library(gt)

gtcars |> 
  dplyr::filter(ctry_origin == "Germany") |> 
  dplyr::select(1:5) |> 
  dplyr::add_row(
    mfr = NA_character_,
    model = "Some great German cars:",
    .before = 1 
  ) |> 
  gt(
    rowname_col = "model",
    groupname_col = "mfr"
  )

image

What I would like to be able to do is something like this:

image

The latter is of course a result of the below code, but for data with a lot of groups and only one NA row, the feature could save a lot of coding:

gtcars |> 
  dplyr::filter(ctry_origin == "Germany") |> 
  dplyr::select(1:5) |> 
  dplyr::add_row(
    mfr = NA_character_,
    model = "Some great German cars:",
    .before = 1 
  ) |>
  gt() |> 
  tab_row_group(
    label = "BMW",
    rows = mfr == "BMW"
  ) |> 
  tab_row_group(
    label = "Audi",
    rows = mfr == "Audi"
  ) |> 
  tab_row_group(
    label = "Mercedes-Benz",
    rows = mfr == "Mercedes-Benz"
  ) |> 
  tab_row_group(
    label = "Porsche",
    rows = mfr == "Porsche"
  ) |> 
  row_group_order(
    groups = c(NA_character_, "BMW", "Audi", "Mercedes-Benz", "Porsche")
  ) |> 
  cols_hide(
    columns = "mfr"
  )

Thanks a lot, Rich!

  • [ x ] For any problems you identify, post a minimal reproducible example so the maintainer can troubleshoot. 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.

Activity

  1. self-assigned this
    on Feb 16, 2023
  2. moved this from Backlog to To discuss / To plan in R Markdown Team Projectson Feb 16, 2023
  3. moved this from To discuss / To plan to Backlog in R Markdown Team Projectson Feb 16, 2023
  4. modified the milestones: v0.10.0, v0.9.0 on Feb 16, 2023
  5. alex-lauer commented on Feb 22, 2023

    @alex-lauer
    CollaboratorAuthor

    For NA values not created by dplyr::add_row(), there's usually some work needed if the NA group should be at the top of the table, rather than at the bottom (dplyr default is to arrange NA's last). Would be perfect if there was an option to choose na.last = TRUE/ FALSE.

  6. modified the milestones: v0.9.0, v0.10.0 on Mar 27, 2023
  7. modified the milestones: v0.10.0, v0.11.0 on Sep 29, 2023
  8. modified the milestones: v0.11.0, v0.12.0 on Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions