0

As title, how do you include subscript/superscript for table to be rendered in Shiny? There seems to be solution for rownames but no question on the column names.

One way is to directly change the column names of dataframe using unicode, such as:

test <- data.frame(
  "\u03C3\u2080" = integer(),
  "\u03C3\u2122" = integer(),
  "\u03b1\u209b\u1d64\u209a" = integer()
)
colnames(test) = c("\u03C3\u2080", "\u03C3\u2122", "\u03b1\u209b\u1d64\u209a")
test

it leaves a lot to be desired as you cannot directly assign some unicode to columnname (redundantly have to use colnames()), and cannot use characters that unicode does not support, because unicode hates alphabet letter q for some reason.

When user is just trying to export dataframe as word doc or something, user can use external package such as kable() to use markdown and display subscript/superscript, but how does one do that in Shiny environment?

output$test <- renderTable({
   test <- data.frame(
      "sigma_zero" = integer(),
      "sigma^tm" = integer(),
      "alpha_sup" = integer()
    )
  #colnames(test) = c("\u03C3\u2080", "\u03C3\u2122", "\u03b1\u209b\u1d64\u209a")
  return(test)
})
aiorr
  • 547
  • 4
  • 11
  • Try the datatable function as part of the DT package. – user330 Aug 22 '23 at 18:43
  • I wanted to use `renderTable()`, but I did look up `renderDT()` for future use, and the solution for that family of rendering was `escape = FALSE` argument, while using HTML as Stephanie answered. – aiorr Aug 23 '23 at 15:46

1 Answers1

1

You can use HTML:

library(shiny)

ui <- fluidPage(
  tableOutput("mytable")
)

server <- function(input, output, session) {

  output$mytable <- renderTable({
    test <- data.frame(
      "sigma_zero" = integer(),
      "sigma^tm" = integer(),
      "alpha_sup" = integer()
    )
    colnames(test) = 
      c("&sigma;<sub>0</sub>", "&sigma;<sup>TM</sup>", "&alpha;<sub>sup</sub>")
    return(test)
  }, sanitize.colnames.function = identity)
  
}

shinyApp(ui, server)

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225