Filtering, captioning and editing in tables with DT



This post explains how to add a filter parameter , a caption and edit cells in tables with the DT package. We’ll go over several examples of how you can display interactive tables with customized features.
You can find out more about DT in the dedicated section.

Table Data to Viz

Packages


For this post, we need to load the following library:

# install it if never done it before
# install.packages("DT")

library(DT)


Dataset


For this post, we’ll use a dataset with different data types such as integer, numeric, factor, logical, and character.

df <- data.frame(
  integer_col = as.integer(c(1, 2, 3)), # Integer column
  numeric_col = c(1.1, 2.2, 3.3), # Numeric column
  factor_col = factor(c("level1", "level2", "level3")), # Factor column
  logical_col = c(TRUE, FALSE, TRUE), # Logical column
  character_col = c("a", "b", "c") # Character column
)

Filtering


By default, DT tables have no filters. However, the datatable() function has a filter argument with very useful properties, depending on the type of data.

Integer and numeric columns have range sliders, while other variables have a choice of all possible values.

Keep in mind: make sure your variables have the right type so that the filters match. You may need to manually change the type of some of your columns.

table <- datatable(df,
  filter = "top"
) # put it at the top of the table

# save widget
library(htmltools)
saveWidget(table, file = "HtmlWidget/dt-filtering.html")

Captioning


Thanks to the caption argument, we can easily add a caption to our table.

table <- datatable(
  df,
  caption = tags$caption(
    style = "caption-side: bottom; text-align: center;",
    "Table 1: ",
    em("This is a legend that you can customize")
  )
)

# save widget
library(htmltools)
saveWidget(table, file = "HtmlWidget/dt-captioning.html")

Editing


The cells can be edited directly into the table by double clicking on it, changing the value and then clicking outside. It’s also possible to make only specific as editable.

In the case you want all cells to be editable, put editable='cell' in the datatable() function. Otherwise, put a list in argument of the editable argument like in the example below that allows editing in column 1, 3 and 5, for all rows:

table <- datatable(df,
  editable = list(
    target = "row",
    disable = list(columns = c(1, 3, 5))
  )
)

# save widget
library(htmltools)
saveWidget(table, file = "HtmlWidget/dt-editing.html")

All in once


Here’s what the output looks like when we put all together the features we’ve seen before:

df <- rbind(df, df, df) # add more rows
table <- datatable(df,
  editable = list(
    target = "row",
    disable = list(columns = c(1, 3, 5))
  ),
  caption = tags$caption(
    style = "caption-side: bottom; text-align: center;",
    "Table 1: ",
    em("This is a legend that you can customize")
  ),
  filter = "top"
)

# save widget
library(htmltools)
saveWidget(table, file = "HtmlWidget/dt-all-features.html")

Conclusion

This post explained how to add a filter parameter, a caption and edit cells in DT table. For more of this package, see the dedicated section or the table section.

Related chart types


Line plot
Area
Stacked area
Streamchart
Time Series



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥