htmltools: creating a spanned footer

...I have a DT table (in a shiny app) that needs a source citation in the table footer. I have found a solution:

>          sketch6 <- htmltools::withTags(table(
>                     tableHeader(outtabpov6),
>                     tableFooter(outCaption6)
>                     ))
>           
>           POVTabOut6 <-datatable(outtabpov6,
>                                  container = sketch6,
>                                  rownames = FALSE,
>                                  caption = paste0("Table 6: Percent Below Federal Poverty Level by Age Trend: ",input$level),
>                                  options = list(pageLength = 12,
>                                                autowidth= TRUE,
>                                                columnDefs = list(list(width = '300px', targets = "_all"))))

However, I need to modify the tableFooter output so that it spans the columns of the table. Currently, the footer created by tableFooter is limited to the space allocated for the first column:

Any ideas about how to modify the tableFooter command to create a spanned footer?
If there is another way to add a formatted footer to a DT, I'd appreciate heading about it.
TIA
AB

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

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.