Search code examples
rr-markdownkablekableextra

Add a superscript to one cell of a table formatted with kable / kableExtra for Rmarkdown


I have a footnote in a table formatted with kable and kableExtra for an Rmarkdown-generated pdf. I need NYC to have the superscript (NYC^1) to point to the footnote, but can't figure out how to do it. Any suggestions?

df <- data.frame(city=c("NYC","LA","CHI","MIA"),
                 score=sample(1:100, 4, replace=T))

library(kableExtra)
library(kable)

kable(df, 
      digits = 2,
      format = "latex",
      align="c",
      row.names = FALSE,
      booktabs=T) %>%
  kable_styling(bootstrap_options = c("hover"),
                full_width = F,
                font_size = 12,
                position = "left") %>%
  footnote(number = c("2017 data missing"))

Solution

  • I don't have the elegant answer for you. Here's a way to hack it by replacing the strings inside the generated latex string.

    library(kableExtra)
    library(stringr)
    
    df <- data.frame(city=c("NYCHACKIT","LA","CHI","MIA"),score=sample(1:100, 4, replace=T))
    
    tmp <- knitr::kable(df,  
          digits = 2,
          format = "latex",
          align="c",
          row.names = FALSE,
          booktabs=T) %>%
      kable_styling(bootstrap_options = c("hover"),
                    full_width = F,
                    font_size = 12,
                    position = "left") %>%
      footnote(number = c("2017 data missing"))
    
    knitr::asis_output(str_replace(tmp, "HACKIT", "$^{1}$"))