【问题标题】:DT datatable caption hyperlinkDT 数据表标题超链接
【发布时间】:2016-02-16 06:15:31
【问题描述】:

是否可以在 Rstudio DT 数据表中在标题参数中添加超链接?我已经尝试过,但我似乎无法得到任何工作。我尝试了 w3schools fiddle 的 html 标题,我可以在表格的标题中获得一个超链接,但我不知道如何将其转换为 DT 数据表。我曾尝试通过 htmltools:: 调用它,但我只能让它呈现为文本,例如:

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

)

【问题讨论】:

    标签: dt


    【解决方案1】:

    我知道这有点老了,但由于我今天遇到了类似的问题并找到了答案,所以我想我会分享。我这样做的方法是使用 Shiny 的 HTML 函数正确编码 html,这将处理必要的转义。可以在这里看到一个例子:

    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>'))
        )
      )
    )
    

    一个简单的 Shiny 应用程序中的完整示例:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2019-04-18
      相关资源
      最近更新 更多