subscripts to annotate gt table?

I'm trying to use subscripts with gt to annotate a table. I tried a few different things and none of them work. Here is the code that I wrote:

tibble(' ' = c("Frequentist", "Bayesian"),
       `Test against null` = c(html("61%<sub>a</sub>"), "84%_b"),
       `Test against MME` = c("57%_{a,c}", "42%_c")) %>% 
  gt() %>%
  tab_source_note(
    md(
      "**Note:** Proportions with different subscripts have at least a 95% probability of differing by at least 10 percentage points."
    )
  )

Is it possible to do what I want? If so, what am I missing?
Thanks!

Hopefully someonce can improve, but I found this hacky solution to work.
I'm using @ and ~ as symbols to represent where I want to text_transform function to substitute in the open and close sub tags. I tried making the text transform just return its input and having your original input with the sub tags in there to start with but it didnt work.

library(tidyverse)
library(glue)
library(gt)
library(stringr)

a<-1
b<-2
c<-3


tibble(' ' = c("Frequentist", "Bayesian"),
       `Test against null` = c(glue("61%@{a}~"), glue("84%_{b}")),
       `Test against MME` = c(glue("57%_{a},{c}"), glue("42%_c"))) %>% 
  gt() %>%
  tab_source_note(
    md(
      "**Note:** Proportions with different subscripts have at least a 95% probability of differing by at least 10 percentage points."
    )
  ) %>%  text_transform(
    locations = cells_body(),
    fn = function(x) {
    str_replace_all(x,
                    pattern = "@",
                    replacement = "<sub>") %>% 
        str_replace_all("~",
                        "</sub>") }
  )
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.