0

How to add subscripts in the row names of a renderTable? In the following example I need subscripts in A_1 and A_2.

library(shiny)

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

server <- function(input, output) {
  output$table <- renderTable({data <- data.frame(c(1, 2),
                                         row.names = c("A_1", "A_2"))}, rownames = T)}

shinyApp(ui = ui, server = server)
Joe
  • 1,628
  • 3
  • 25
  • 39

1 Answers1

3

You can use DT package for datatable for that. You need to use html tags with escape = FALSE. Have a look at the modified your code below:

  library(shiny)
  library(DT)
  ui <- fluidPage(dataTableOutput("table"))

  server <- function(input, output) {
    output$table <- renderDataTable({
      data <- datatable(data.frame(c(1, 2), row.names = c("A<sub>1</sub>", "A<sub>2</sub>")), rownames = T, escape = FALSE)
      })
  }

  shinyApp(ui = ui, server = server)

You get a table which looks like this:

enter image description here

EDIT:

You can add subscript for renderTable by using html tags with sanitize.text.function = function(x) x. The code would be as shown below:

 library(shiny)

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

    server <- function(input, output) {
      output$table <- renderTable({data <- data.frame(c(1, 2),
                                                      row.names = c("A<sub>1</sub>", "A<sub>1</sub>"))}, rownames = T, sanitize.text.function = function(x) x)}

    shinyApp(ui = ui, server = server)

The output table will look as follows: enter image description here

Hope it helps!

SBista
  • 7,479
  • 1
  • 27
  • 58
  • The layout of `datatable` does not suit my purpose. I am looking for a solution with `rendertable`. – Joe Oct 12 '17 at 16:40
  • 2
    @Joe I have edited my answer with solution for `renderTable`. Hope it helps! – SBista Oct 18 '17 at 11:03