You are almost there. tags$caption from the htmltools package will help you style the caption. In your example, it would look like this.
caption = htmltools::tags$caption(
style="caption-side: bottom; text-align: left; margin: 8px 0;",
"Table 6: Percent Below Federal Poverty Level by Age Trend: ",input$level
)
Use caption-side: bottom if you want the caption placed after the table. I set the text align to left and added a slight margin for some breathing room.
Here's a full working example with the iris dataset and column names as the footer.
# pkgs
libray(DT)
library(htmltools)
# build container
table <- withTags(
tags$table(
DT::tableHeader(names(iris)),
DT::tableFooter(names(iris))
)
)
# build data
out <- DT::datatable(
iris,
container = table,
rownames = FALSE,
caption = tags$caption(
style="caption-side: bottom; text-align: left; margin: 8px 0;",
"this is a caption"
),
options = list(
pageLength = 12,
autowidth= TRUE,
columnDefs = list(list(width = '300px', targets = "_all"))
)
);
# print table
out
There are more options for customizing the caption element. See JS library docs for more info: https://datatables.net/blog/2014-11-07