1

I'm trying to add a superscript to a DataTables footer in a Shiny app (similar as here to the rownames/body).

library(shiny)
library(DT)

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

server <- function(input, output) {
  output$table <- renderDataTable({

    sketch <- htmltools::withTags(table(
      tableFooter(c(("Footer<sup>1</sup>"),
                     "Footer<sup>2</sup>"))
    ))

    data <- datatable(data.frame(c(1, 2),
                                 row.names = c("A<sub>1</sub>",
                                               "A<sub>2</sub>")),
                      rownames = TRUE,
                      container = sketch,
                      escape = FALSE)
  })
}

shinyApp(ui = ui, server = server)

However, instead of rendering to supscript, the tag is printed literally:

Having a look at sketch shows that the greater/lesser than symbols are converted into their HTML character entities:

> sketch
<table>
  <tfoot>
    <tr>
      <th>Footer&lt;sup&gt;1&lt;/sup&gt;</th>
      <th>Footer&lt;sup&gt;2&lt;/sup&gt;</th>
    </tr>
  </tfoot>
</table>

I unsuccessfully tried to escape them with \ and \\ and htmltool's manual on withTags() is quite sparse. So, how is it possible to escape the greater/lesser signs to preserve them as tags? Thank you already! :)

2 Answers 2

1

It would be best to use the additional escape argument in the tableFooter function.

library(shiny)
library(DT)

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

server <- function(input, output) {
  output$table <- renderDataTable({
    sketch <- htmltools::withTags(
      table(
        tableFooter(c(
          "Footer<sup>1</sup>",
          "Footer<sup>2</sup>"
        ), escape = FALSE)
      )
    )

    data <- datatable(
      data.frame(c(1, 2),
        row.names = c(
          "A<sub>1</sub>",
          "A<sub>2</sub>"
        )
      ),
      rownames = TRUE,
      container = sketch,
      escape = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

enter image description here

0
1

Instead of withtags we could use tags:

library(shiny)
library(DT)

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

server <- function(input, output) {
  output$table <- renderDataTable({
    
    sketch <- tags$table(
      tags$tfoot(
        tags$tr(
          tags$th(
            tags$p(
              HTML("Footer<sup>1</sup>",
                   "Footer<sup>2</sup>"),
              style = "display: inline;"
            )
          )
        )
      )
    )
    
    data <- datatable(
      data.frame(c(1, 2),
                 row.names = c("A<sub>1</sub>", "A<sub>2</sub>")),
      rownames = TRUE,
      container = sketch,
      escape = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.