首页 > 解决方案 > gt表R正文中的下标和上标

问题描述

如何在 R 中获取我的 gt 表以显示下标/上标?我希望同位素列中的数字显示为上标。

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

标签: rsubscriptsuperscriptgt

解决方案


实现所需结果的一种选择是使用gt::text_transformHTML<sup>标签,如下所示:

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}")
    }
  )

在此处输入图像描述


推荐阅读