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.
For this post, we need to load the following library:
For this post, we’ll use a dataset with different data types such as integer, numeric, factor, logical, and character.
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.
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:
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")
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.
👋 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! 🔥