Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling missing values in a col_merge #1118

Closed
erhoppe opened this issue Nov 10, 2022 · 2 comments · Fixed by #1144
Closed

Handling missing values in a col_merge #1118

erhoppe opened this issue Nov 10, 2022 · 2 comments · Fixed by #1144

Comments

@erhoppe
Copy link

erhoppe commented Nov 10, 2022

Thanks for the great package! It's super useful. I wondered if it would be possible to add an option for dealing with missing data in cols_merge so that you could either override sub_missing or somehow provide an alternative pattern for cells where you only have missing data.

I'm encountering a situation where I have a subgroup of data for a particular column that is missing data, so my summary with the median, min, and max are all missing. I would like to just have a single "n.p" in that cell rather, but currently, it doesn't appear as if this is possible.

library(tidyverse)
library(gt)

tibble(
  category = c("a","b","c","d"),
  median = c(5,10,8, NA_real_),
  min = c(1,6,4,NA_real_),
  max = c(8,25,12,NA_real_),
) %>% 
  gt(rowname_col = "category") %>% 
  cols_merge(columns = c(median, min, max),
             pattern = "{1}<br>({2},{3})") %>%
  tab_spanner(
    label = "Measurement",
    columns = c(median)
  ) %>% 
  cols_label(
    median = html("Median<br>(min,max)")
  ) %>% 
  sub_missing(columns =
                c(median),
              rows = everything(), missing_text = "n.p.") %>%
  sub_missing(columns =
                c(min,max),
              rows = everything(), missing_text = "") %>% 
  tab_source_note(
    source_note = "n.p. = annotation not provided by authors"
  )

Screen Shot 2022-11-10 at 1 15 29 AM

Would something like this be possible?

@rich-iannone
Copy link
Member

Thanks for filing this issue and providing a great example! This has come up a few times and I think we need some extra syntax in the pattern to enclose a run of text. Where any NAs are encountered, the entire span would be omitted.

@erhoppe
Copy link
Author

erhoppe commented Nov 10, 2022

That sounds like a great solution for it. I look forward to seeing what you come up with. :) Thanks again!

Repository owner moved this from To discuss / To plan to Done in R Markdown Team Projects Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment