R 中 DT 中的多个 table 字幕

Muliple table captions in DT in R

我正在按照此处的示例进行操作:https://rstudio.github.io/DT/

我可以在 table 上方添加标题:

library(DT)

datatable(
  head(iris),
  caption = 'Table 1: This is a simple caption for the table.'
)

table 下方的标题为:

library(DT)

datatable(
  head(iris),
  caption = htmltools::tags$caption(
    style = 'caption-side: bottom; text-align: center;',
    'Table 2: ', htmltools::em('This is a simple caption for the table.')
  )
)

我怎么能同时有两个字幕(上方和下方)?

干杯, 凯特

您可以进行如下操作:

library(DT)

js <- c(
  "function(settings){",
  "  var datatable = settings.oInstance.api();",
  "  var table = datatable.table().node();",
  "  var caption = 'ANOTHER CAPTION'",
  "  $(table).append('<caption style=\"caption-side: bottom\">' + caption + '</caption>');",
  "}"
)

datatable(
  head(iris),
  options = list(
    drawCallback = JS(js)
  ),
  caption = 'Table 1: This is a simple caption for the table.'
)

很好地使用 JS() 在生成 table 后附加新的标题。当我将其与具有分页的 table 一起使用时,每次选择数据 table 的不同页面时都会附加标题。为了避免连续追加,我建议使用 'initComplete' 而不是 'drawCallback' 作为选项。摘自 Stephane Laurent 的代码:

library(DT)

js <- c(
  "function(settings){",
  "  var datatable = settings.oInstance.api();",
  "  var table = datatable.table().node();",
  "  var caption = 'ANOTHER CAPTION'",
  "  $(table).append('<caption style=\"caption-side: bottom\">' + caption + '</caption>');",
  "}"
)

datatable(
  head(iris),
  options = list(
    initComplete = JS(js) #This will avoid looping of the caption when there is pagination
  ),
  caption = 'Table 1: This is a simple caption for the table.'
)