A.Jehu
A.Jehu

Reputation: 33

DT datatable caption hyperlink

Is it possible in Rstudio DT datatable to add a hyperlink in the caption argument? I have tried and tried but I can't seem to get anything working. I tried the w3schools fiddle for html caption and I can get a hyperlink to work in the caption of the table but I don't know how to translate this to the DT datatable. I have tried calling it via htmltools:: but I can only get it to render as text for example:

datatable(tble
,caption =  
htmltools::tags$caption(
style = 'caption-side: top; text-align: left; color:blue; font-size: 12px;',
,htmltools::p('<!DOCTYPE html>
<html><body><a href="http://rstudio.com">RStudio</a></body>
</html>'))
,escape = FALSE

)

Upvotes: 2

Views: 2077

Answers (1)

J Hamburg
J Hamburg

Reputation: 151

I know this is a little old but since I had a similar issue today and figured out an answer, I figured I would share. The way I did this was to use the HTML function from Shiny to encode the html correctly, which will take care of the necessary escaping. An example can be seen here:

DT::datatable(
  get(input$dataInput),
  caption = htmltools::tags$caption(
    style = 'caption-side: top; text-align: Left;',
    htmltools::withTags(
      div(HTML('Here is a link to <a href="http://rstudio.com">RStudio</a>'))
    )
  )
)

A full example of this in a simple Shiny application:

library(shiny)
library(DT)

data("mtcars")
data("iris")

ui <- fluidPage(
  titlePanel("Example Datatable with Link in Caption"),
  selectInput('dataInput', 'Select a Dataset', 
              c('mtcars', 'iris')),
  DT::dataTableOutput('example1')
)

server <- function(input, output, session){
  output$example1 <- DT::renderDataTable({
    # Output datatable
    DT::datatable(
      get(input$dataInput),
      caption = htmltools::tags$caption(
        style = 'caption-side: top; text-align: Left;',
        htmltools::withTags(
          div(HTML('Here is a link to <a href="http://rstudio.com">RStudio</a>'))
        )
      )
    )
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 5

Related Questions