Repository navigation
Option to omit groupname_cols with NA values #1215
Description
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"
)What I would like to be able to do is something like this:
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.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone


Activity
alex-lauer commentedon Feb 22, 2023
For
NAvalues not created bydplyr::add_row(), there's usually some work needed if theNAgroup 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 choosena.last = TRUE/ FALSE.