Gt: Feature Request: cols_merge handling of missing values

Created on 19 Apr 2019  ·  8Comments  ·  Source: rstudio/gt

Hi Rich, Thank you for your amazing work on this package!

I am using the cols_merge() function, and I wanted to put out a proposal for an enhancement regarding missing value handling. When merging two columns if one of the two columns is missing, I may want to see the merged column with the missing value in the output (like glue() where glue("{5}, {NA}"), resolves to 5, NA).

Here's an example:

This is what the default output looks like

library(tidyverse)
library(glue)
library(gt)
tbl <-
  tribble(
    ~est, ~ll, ~ul,
    10, 9,  11,
    12, 11, NA,
    12, NA, 13,
    14, NA, NA
  ) 

tbl
#> # A tibble: 4 x 3
#>     est    ll    ul
#>   <dbl> <dbl> <dbl>
#> 1    10     9    11
#> 2    12    11    NA
#> 3    12    NA    13
#> 4    14    NA    NA

# current output from cols_merge
tbl %>%
  gt() %>%
  cols_merge(col_1 = "ll", col_2 = "ul", 
             pattern = "{1}, {2}")

image

Perhaps by adding a missing option, the users could indicate they'd like to see the NA values in the merged cell. In the example below, missing values would be replaced by an em dash.

tbl %>%
  gt() %>%
  cols_merge(col_1 = "ll", col_2 = "ul", 
             pattern = "{1}, {2}",
             missing = "—")

image

THANK YOU! 🍁

[2] Intermediate [2] Medium [2] Medium ★ Enhancement

Most helpful comment

@ddsjoberg I moved the remaining (great!) ideas you provided (with some other variations) to https://github.com/rstudio/gt/issues/603 and I'm closing this. It's good to have a fresh start! Feel free to comment on whether the new stuff makes sense.

All 8 comments

Thanks for posting this issue. Makes a lot of sense to me and I really like the idea of a missing option. We will get to this soon!

After https://github.com/rstudio/gt/pull/355 is merged, the following statements will produce something similar to what you described:

library(tidyverse)
library(gt)

tbl <-
  dplyr::tribble(
    ~est, ~ll, ~ul,
    10, 9,  11,
    12, 11, NA,
    12, NA, 13,
    14, NA, NA
  ) 

tbl %>%
  gt() %>%
  fmt_missing(columns = everything(), missing_text =  "—") %>%
  cols_merge(
    columns = c("ll", "ul"), 
    pattern = "{1}, {2}",
    hide_columns = "ul"
  )

cols_merge_redux

This looks awesome!! Thank you so much!!!

Hi Rich,

I was playing around with the new cols_merge() from #355 and it's great!

One thing that would be wonderful to add is a rows = argument so we can conditionally merge columns for certain rows only (similar to what is available in other gt functions, eg fmt_missing()).

I am using cols_merge to summarize regression models.
image
I merge the upper and lower bounds of the confidence interval of the regression coefficient. But on the row that is the variable label, I would like to keep it blank. For the row showing the reference group (stage T1 in the example), I want to show an emdash to indicate this is the reference group.

Is that something that is possible to implement?

Again, thank you for the world's best package :)

Yes! I very thinking of this today (or was it yesterday? don't quite remember)... it's a great idea. There are actually a bunch of places where there are columns args and no corresponding rows and I think they should all be addressed. I do feel that the cols_merge functions should get this enhancement first though.

Thanks again for your input here! (And for the good words.)

You are so on top of it!! If rows = update could be incorporated with PR #355, that would be amazing ⚡️ Otherwise, I don't think there is a way to have a blank cell in the same column as a merged column.

@ddsjoberg I moved the remaining (great!) ideas you provided (with some other variations) to https://github.com/rstudio/gt/issues/603 and I'm closing this. It's good to have a fresh start! Feel free to comment on whether the new stuff makes sense.

Thanks @rich-iannone !! 🍁{gt}🍁

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vikram-rawat picture vikram-rawat  ·  3Comments

steveputman picture steveputman  ·  3Comments

jthomasmock picture jthomasmock  ·  3Comments

courtiol picture courtiol  ·  3Comments

r2evans picture r2evans  ·  5Comments