itsMeInMiami
itsMeInMiami

Reputation: 2773

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() 

Upvotes: 1

Views: 586

Answers (2)

Tech Commodities
Tech Commodities

Reputation: 1959

Easiest solution is to use the unicode values, if one exists. For the squared symbol it's "\U00B2":

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

enter image description here

Upvotes: 3

Matt
Matt

Reputation: 7413

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:

Upvotes: 2

Related Questions