How do I get my gt table in R to show subscripts/superscripts? I would like the numbers in the isotope column to display as superscripts.
library(gt)
Isotope <- c("1H", "2H", "16O", "17O", "18O")
Abundance <- c(0.99985, 0.00015, 0.99757, 0.00038, 0.00205)
table <- as.data.frame(cbind(Isotope, Abundance))
table %>% gt()
One option to achieve your desired result would be to make use of gt::text_transform
and the HTML <sup>
tag like so:
library(gt)
library(stringr)
Isotope <- c("1H", "2H", "16O", "17O", "18O")
Abundance <- c(0.99985, 0.00015, 0.99757, 0.00038, 0.00205)
table <- data.frame(Isotope, Abundance)
table %>% gt() %>%
text_transform(
locations = cells_body(
columns = c(Isotope)
),
fn = function(x){
sup <- str_extract(x, "^\\d+")
text <- str_extract(x, "[^\\d]+")
glue::glue("<sup>{sup}</sup>{text}")
}
)