Search code examples
rrowgt

How do I edit row text to include special symbols and functions in a {gt} table?


I'm trying to add units to the variables contained en rows in my table, some of which include greek symbols and ^2. The greek symbols are turned ? in my gt-table, if I try to achieve this in my dataset before creating the table.

I've seen this done for column labels, but I havn't found a way to format row text.

I'm including this code as an example:

library(tidyverse)
library(gt)

df <- data.frame(cbind(c('BMI', 'TG', 'ApoB48'),c(21.6,0.62,4.58),c(22.5,0.91,5.77),c(23.6,0.99,6.95)))

gt(df, rowname_col = "Variable") %>%
  fmt_markdown(columns = 1) %>%
  tab_header(title = "Participant characteristics and measured biomarkers"
  ) %>%
  cols_label(
    X1 = "Variable",
    X2 = "Q1",
    X3 = "Median",
    X4 = "Q3"
  )

In the 'Variable' column, I would like the row containing BMI to say BMI(kg/m²), TG to say TG (mmol/L), and ApoB48 to say ApoB48 (μmol/L).

Thank you so much in advance!


Solution

  • You can wrap your labels in html to use with gt tables. For example, for superscript use <sup>/</sup> tags, and for greek letter use &mu;. Just for demonstration, I included these in your data.frame for testing.

    df <- data.frame(
      cbind(
        c(html('BMI (kg/m<sup>2</sup>)'), 'TG (mmol/L)', html('ApoB48 (&mu;mol/L)')),
        c(21.6,0.62,4.58),c(22.5,0.91,5.77),c(23.6,0.99,6.95)
      )
    )