Edit Row Group Labels in GT

I'm creating a poster for a conference which uses data about kidney function and potassium levels.

I've created a summary table of data from within a much larger dataset and am trying to use the gt() package to create my tables.

(I'm a doctor, without formal data science training, and this is my first time asking a question (I've always been able to google the answers until now), I've tried to make a reproducible example but please go easy on me if I've not provided enough information!)

I created this data set by grouping according to levels of kidney function (ckdlevel) and maximum potassium (Max K+)

I managed (eventually) to figure out how to convert >= into so that it formatted properly

I want to convert mls/min/1.73m2 into mls/min/1.73m 2, but I get the following error message.

One option I've figured out is to use a footnote, but my supervisor is not keen on this. Is there a way to fix this?

library(tidyverse)
library(gt)
data <- structure(list(
  ckdlevel = c("<15 mls/min/1.73m^2", "<15 mls/min/1.73m^2", 
               "15-29 mls/min/1.73m^2", "15-29 mls/min/1.73m^2", "30 - 44 mls/min/1.73m^2", 
               "30 - 44 mls/min/1.73m^2", "45 - 59 mls/min/1.73m^2", "45 - 59 mls/min/1.73m^2", 
               ">60 mls/min/1.73m^2", ">60 mls/min/1.73m^2", "Missing", "Missing"
  ), 
  `Max K+` = c(" ltoet5 mmol/l", ">5 mmol/l", " ltoet5 mmol/l", 
               ">5 mmol/l", " ltoet5 mmol/l", ">5 mmol/l", " ltoet5 mmol/l", 
               ">5 mmol/l", " ltoet5 mmol/l", ">5 mmol/l", " ltoet5 mmol/l", 
               ">5 mmol/l"), 
  n = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 800, 700, 600)), 
  row.names = c(NA, -12L), 
  class = c("tbl_df", "tbl", "data.frame"))

data %>% 
  group_by(ckdlevel) %>% 
  gt() %>% 
    text_transform(
    locations = cells_body(
      columns = "Max K+"),
       fn = function(x) {
str_replace(x, pattern = "ltoet", replacement = "<span>&#8804;</span>")
    }
  ) %>% 
   text_transform(
    locations = cells_row_groups(
              groups = everything()),
       fn = function(x) {
str_replace(x, pattern = "^2", replacement = "<sup>2</sup>")
    }
  ) 
#> Error in UseMethod("resolve_location"): no applicable method for 'resolve_location' applied to an object of class "c('cells_row_groups', 'location_cells')"

Thank you!

I think two things went wrong here.
The most important one is that gt until now does not support text_transform on 'row group labels'.
See e.g. this issue .

Also, while I am not very familiar with regular expressions, I think that you should escape the ^ symbol in "^2".
Apart from this your solution should work. See the code below.
The only possibility I see now is to use the regular expression on the html-file that is produced.
To be on the save side I also include the </td> part of the cell in the example below.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(stringr)
library(gt)

df1 <- data.frame(
  cat1 = c( "1 m^2", "1 m^2", "2 m^2", "2 m^2"),
  cat2 = c(1, 2, 1, 2),
  value = 1:4
)

df2 <- df1 %>% 
  group_by(cat1) %>%
  mutate (cat1=str_replace(cat1, pattern = "\\^2", replacement = "<sup>2</sup>") , 
          cat1=purrr::map(cat1,gt::html) ) 

gt2 <- df2 %>%
 gt() 
# gt2 shows row group labels as "1 m<sup>2</sup>" 

df3 <- df1 %>% 
  mutate (cat1=str_replace(cat1, pattern = "\\^2", replacement = "<sup>2</sup>") , 
          cat1=purrr::map(cat1,gt::html) ) 

gt3 <- df3 %>%
 gt()
# gt3 shows squared meters correctly (but as simple row cells not as group labels)

# final 'solution' : correct the html-file
gt_dummy <- df1 %>% 
  gt() %>%
  gtsave("hugh_le.html",inline_css=T)
  
html_lines <- readLines("hugh_le.html",warn=F)
html_lines <- str_replace_all(html_lines, pattern = " m\\^2</td>", replacement = " m<sup>2</sup></td>")
writeLines(html_lines,"hugh_le.html")
Created on 2021-09-09 by the reprex package (v2.0.0)
1 Like

Thank you very much for going to all the trouble! That's marvellous

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: