【问题标题】:Subscripts and superscripts in body of gt table Rgt表R正文中的下标和上标
【发布时间】:2021-11-13 07:19:14
【问题描述】:

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

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

【问题讨论】:

    标签: r subscript superscript gt


    【解决方案1】:

    实现所需结果的一种方法是使用gt::text_transform 和HTML &lt;sup&gt; 标签,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多