Search code examples
rgt

With the r gt package how can I print a simple superscript in the body of a table


I would like to include a superscript in one cell of a table printed with the gt package. I found an example that shows how to print superscripts as part of a more complicated workflow but I just need a single superscript. Is there an easy way to print R with a superscript 2 in the table created here:

library(dplyr)
library(gt)


`table3-2` <- tribble(
  ~Quantity, ~Value,
  "Residual standard error", 3.26,
  "R2", 0.612,
  "F-statistic", 312.1
)

`table3-2` %>% 
  gt() 

Solution

  • You just need to supply a rows argument to text_transform:

    `table3-2` %>% 
      gt() %>% 
      text_transform(
        locations = cells_body(
          columns = c(Quantity),
          rows = 2
        ),
        fn = function(x){
          sup <- 1
          text <- "R2"
          glue::glue("<sup>{sup}</sup>{text}")
        }
      )
    

    This adds a superscript to R2 and gives us: