knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>"
)
library(dtf)
logger::log_threshold(logger::TRACE)

AutoFill

See https://rstudio.github.io/DT/extensions.html (setion 1. AutoFill)

With the AutoFill extension, you will see a blue square in the bottom-r^ight corner of a cell when you mouse over the cell. You can drag it to automatically fill the column.

Default

dt_bundle_autofill()
dt_bundle_autofill()
mtcars %>% datatable2(bundle = dt_bundle_autofill(), .verbose = TRUE)
mtcars %>% datatable2(bundle = "AutoFill")

For comparison when calling DT::datatable()

mtcars %>% DT::datatable(
    extensions = "AutoFill", 
    options = list(
        autoFill = TRUE
    )
)

Custom options

Pass some initialization options to AutoFill: only enable auto filling on the columns 2, 3, 4; show the blue square on click instead of hover.

dt_bundle_autofill(columns = c(1, 2, 3))
dt_bundle_autofill(.options = list(columns = c(1, 2, 3), focus = "click"))
dt_bundle_autofill(columns = c("mpg", "disp"), .data = mtcars)
mtcars %>% datatable2(
    bundles = dt_bundle_autofill(columns = c(1, 2, 3))
)
mtcars %>% datatable2(
    bundles = dt_bundle_autofill(columns = c(1, 2, 3), focus = "click")
)
mtcars %>% datatable2(
    bundles = dt_bundle_autofill(
        columns = c("mpg", "disp"), 
        focus = "click",
        .data = mtcars
    )
)
mtcars %>% datatable2(
    bundles = list(
        dt_bundle_autofill(
            .options = list(columns = c(1, 2, 3), focus = "click")
        )
    )
)

For comparison when calling DT::datatable()

mtcars %>% DT::datatable(
    extensions = "AutoFill", 
    options = list(
        autoFill = list(columns = c(1, 2, 3), focus = "click")
    )
)

Buttons

See https://rstudio.github.io/DT/extensions.html (setion 1. AutoFill)

Default

dt_bundle_buttons()
mtcars %>% datatable2(bundles = dt_bundle_buttons(), .verbose = TRUE)
mtcars %>% datatable2(bundles = dt_bundle_buttons_de(), .verbose = TRUE)
mtcars %>% datatable2(bundles = dt_bundle_buttons_en(), .verbose = TRUE)

Custom options

mtcars %>% datatable2(
    bundles = dt_bundle_buttons(buttons = c("copy", "pdf"))
)
mtcars %>% datatable2(
    bundles = dt_bundle_buttons("copy", "pdf")
)
mtcars %>% datatable2(
    bundles = dt_bundle_buttons_de("copy", "pdf")
)
mtcars %>% datatable2(
    bundles = dt_bundle_buttons_en("copy", "pdf")
)
mtcars %>% datatable2(
    bundles = dt_bundle_buttons(
        .options = list(
            buttons = c("copy", "csv")
        )
    )
)

For comparison when calling DT::datatable()

mtcars %>% DT::datatable(
    extensions = "Buttons", 
    options = list(
        dom = "Bfrtip",
        buttons = list(
            "copy",
            "print", 
            list(
                extend = "collection",
                buttons = c("csv", "excel", "pdf"),
                text = "Download"
            )
        )
    )
)

ColReorder

See https://rstudio.github.io/DT/extensions.html (setion 3. ColReorder)

Default

dt_bundle_colreorder()
mtcars %>% datatable2(
    bundles = dt_bundle_colreorder()
)

Custom options

dt_bundle_colreorder(realtime = TRUE)
dt_bundle_colreorder(.options = list(realtime = TRUE))
mtcars %>% datatable2(
    bundles = dt_bundle_colreorder(realtime = TRUE)
)
mtcars %>% datatable2(
    bundles = dt_bundle_colreorder(
        .options = list(realtime = TRUE)
    )
)

FixedColumns

See https://rstudio.github.io/DT/extensions.html (setion 5. FixedColumns) and https://datatables.net/reference/option/fixedColumns.leftColumns

When a table is too wide, you can fix certain columns (e.g. the left and/or the right column) using the FixedColumns extension so that they will be visible when you scroll the table horizontally.

Default

dt_bundle_fixedcolumns()
mtcars %>% datatable2(
    bundles = dt_bundle_fixedcolumns()
)

Custom options

dt_bundle_fixedcolumns(left = 2)
dt_bundle_fixedcolumns(left = "cyl", .data = mtcars)
dt_bundle_fixedcolumns(left = "carb", .data = mtcars)
dt_bundle_fixedcolumns(left = "mpg", right = "gear", .data = mtcars)
dt_bundle_fixedcolumns(.options = list(left = 2, right = 1))
mtcars %>% datatable2(
    bundles = dt_bundle_fixedcolumns(left = 2)
)
mtcars %>% datatable2(
    bundles = dt_bundle_fixedcolumns(left = 1, right = 1)
)
mtcars %>% datatable2(
    bundles = dt_bundle_fixedcolumns(
        left = "mpg", right = "gear", .data = mtcars
    )
)

Custom .options object

mtcars %>% datatable2(
    bundles = dt_bundle_fixedcolumns(
        .options = list(left = 2, right = 1)
    )
)

FixedHeader

See https://rstudio.github.io/DT/extensions.html (setion 6. FixedHeader)

You may want the table header to be always visible when scrolling down the table, and you can use the FixedHeader extension in this case.

Default

dt_bundle_fixedheader()
mtcars %>% datatable2(
    bundles = dt_bundle_fixedheader()
)

Custom options

dt_bundle_fixedheader(fixedHeader = FALSE)
dt_bundle_fixedheader(pageLength = 10)
mtcars %>% datatable2(
    bundles = dt_bundle_fixedheader(pageLength = 15)
)

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_fixedheader(
            .options = list(fixedHeader = TRUE)
        )
    )
mtcars %>% dplyr::bind_rows(mtcars) %>% 
    dplyr::add_rownames() %>% 
    datatable2(
        bundles = dt_bundle_fixedheader(
            .options = list(fixedHeader = TRUE)
        )
    )

KeyTable

See https://rstudio.github.io/DT/extensions.html (setion 7. KeyTable)

The KeyTable extension provides the ability of navigating through the table like Excel: a cell is highlighted by a box, and you can move around using the arrow keys (Left/Right/Up/Down).

Default

dt_bundle_keytable()
mtcars %>% datatable2(
    bundles = dt_bundle_keytable()
)

Custom options

dt_bundle_keytable(keys = FALSE)
mtcars %>% datatable2(
    bundles = dt_bundle_keytable(keys = FALSE)
)

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_keytable(
            .options = list(keys = FALSE)
        )
    )

Responsive

See https://rstudio.github.io/DT/extensions.html (setion 8. Responsive)

The Responsive extension makes the table columns responsive in the sense that when the page is too narrow, certain columns can be automatically collapsed and hidden. In the example below, as you decrease the page width, you will see a sequence of columns collapsed and a button appear on the left. You can click the button to expand the data of the hidden columns behind it.

Default

dt_bundle_responsive()
mtcars %>% datatable2(
    bundles = dt_bundle_responsive()
)

Custom options

dt_bundle_responsive(responsive = FALSE)
mtcars %>% datatable2(
    bundles = dt_bundle_responsive(responsive = FALSE)
)

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_responsive(
            .options = list(responsive = FALSE)
        )
    )

RowGroup

See https://rstudio.github.io/DT/extensions.html (setion 9. RowGroup)

The RowGroup extension provides an easy way to use the row grouping feature. In the example below, cyl (number of cylinders) is used as the row group column and its value is displayed as the row group label.

Default

dt_bundle_rowgroup()
mtcars %>% datatable2(
    bundles = dt_bundle_rowgroup()
)

Custom options

dt_bundle_rowgroup(dataSrc = 2)
dt_bundle_rowgroup(dataSrc = "mpg", .data = mtcars)
mtcars %>% datatable2(
    bundles = dt_bundle_rowgroup(dataSrc = 2)
)
mtcars %>% datatable2(
    bundles = dt_bundle_rowgroup(dataSrc = "disp", .data = mtcars)
)

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_rowgroup(
            .options = list(dataSrc = 2)
        )
    )

RowReorder

See https://rstudio.github.io/DT/extensions.html (setion 10. RowReorder)

The RowReorder extension allows you to drag and reorder a certain row in the table. Note the first column ID is not changed when you reorder the rows in the example below, which can be a little confusing.

Default

dt_bundle_rowreorder()
mtcars %>% datatable2(
    bundles = dt_bundle_rowreorder()
)

Custom options

dt_bundle_rowreorder(rowReorder = FALSE)
mtcars %>% datatable2(
    bundles = dt_bundle_rowreorder(rowReorder = FALSE)
)

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_rowreorder(
            .options = list(rowReorder = FALSE)
        )
    )

Scroller

See https://rstudio.github.io/DT/extensions.html (setion 11. Scroller)

When the table has a large number of rows, you may not want to render all rows on the page immediately since it will be slow. The Scroller extension makes it possible to only render the visible portion of the table.

Default

dt_bundle_scroller()
mtcars %>% datatable2(
    bundles = dt_bundle_scroller()
)

Custom options

dt_bundle_scroller(scrollY = 100)
mtcars %>% dplyr::add_rownames() %>% 
    datatable2(
        bundles = dt_bundle_scroller(scrollY = 100)
    )

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_scroller(
            .options = list(scroller = FALSE)
        )
    )

SearchPanes

See https://rstudio.github.io/DT/extensions.html (setion 12. SearchPanes)

The SearchPanes extension is a new extension that adds panes to the DataTable with the capability to search the DataTable by selecting rows in the panes. As documented in the realsing blog, it doesn’t support the server-side processing mode. Since it depends on the Select extension, you are expected to always include the Select extension with SearchPanes. In addition, in order to display the search panes, you need to insert P to the dom option.

Default

dt_bundle_searchpanes()
mtcars %>% datatable2(
    bundles = dt_bundle_searchpanes()
)

Custom options

dt_bundle_searchpanes(targets = 1:4)
dt_bundle_searchpanes(targets = "mpg", .data = mtcars)
mtcars %>%
    datatable2(
        bundles = dt_bundle_searchpanes(targets = 1:10)
    )
mtcars %>%
    datatable2(
        bundles = dt_bundle_searchpanes(targets = 11)
    )
mtcars %>%
    datatable2(
        bundles = dt_bundle_searchpanes(targets = "carb", .data = mtcars)
    )
# mtcars %>%
#     datatable2(
#         bundles = dt_bundle_searchpanes(collapse = TRUE)
#     )

Custom .options object

try(mtcars %>%
        datatable2(
            bundles = dt_bundle_searchpanes(
                .options = list(show = FALSE)
            )
        ))
mtcars %>% DT::datatable(
    extensions = c('Select', 'SearchPanes'),
    options = list(dom = 'Pfrtip', columnDefs = list(list(
        searchPanes = list(show = FALSE), 
        targets = 1:10
        # targets = c(1:2, 5)
    ))),
    selection = 'none'
)

Select

The Select extension allows the user to perform item selections in DataTable. It supports three different selection styles including os(Operating system), single(Single item select) and multiple(Multi-item selection) and has built-in button types for the Buttons extension allowing select all / none and other actions to be performed very easily. However, it only works well in the client-side processing mode, i.e., DT::renderDT(..., server = FALSE). Note that you need to turn off DT’s own select functionality by setting selection = 'none' when you want to use the Select extension in shiny.

DT also has its own implementation of row/column/cell selections in Shiny (see the selection param in ?DT::datatable), which is different from the Select extension. In particular, it only supports Shift/Ctrl + Click for selecting multiple rows at the time of writing (see #305 for an example). On the other hand, DT’s implementation supports both client and server-side processing mode.

Default

dt_bundle_select()
mtcars %>% datatable2(
    bundles = dt_bundle_select()
)

Custom options

dt_bundle_select("selectRows", "selectColumns")
mtcars %>%
    datatable2(
        bundles = dt_bundle_select("selectRows", "selectColumns", "selectCells")
    )

Custom .options object

try(mtcars %>%
        datatable2(
            bundles = dt_bundle_select(
                .options = list()
            )
        ))
# TODO: provide example for ':options'
mtcars %>% DT::datatable(
    extensions = c('Select', 'Buttons'), 
    options = list(
        select = list(style = 'os', items = 'row'),
        dom = 'Blfrtip',
        rowId = 0,
        buttons = c(
            'selectAll', 
            'selectNone', 
            'selectRows', 
            'selectColumns', 
            'selectCells'
        )
    ),
    selection = 'none'
)

Combine bundles

mtcars %>% datatable2(
    bundles = list(
        dt_bundle_fixedcolumns(), 
        dt_bundle_buttons())
)
mtcars %>% datatable2(
    bundle = c("FixedColumns", "Buttons")
)
mtcars %>% datatable2(
    bundles = list(
        dt_bundle_fixedcolumns(left = 2, right = 1), 
        dt_bundle_buttons(
            .options = list("copy", "csv")
        )
    )
)

[Option] lengthMenue

Default

dt_bundle_lengthmenue()
mtcars %>% datatable2(
    bundles = dt_bundle_lengthmenue()
)

Custom options

dt_bundle_lengthmenue(pageLength = 100)
mtcars %>%
    datatable2(
        bundles = dt_bundle_lengthmenue(pageLength = 100)
    )

Custom .options object

mtcars %>%
    datatable2(
        bundles = dt_bundle_lengthmenue(
            .options = list(pageLength = 15)
        )
    )

Explicit options don't seem to work yet because the mechanism differs from "true" bundles (as opposed to "just" options)


[Option] Internationalization

Option only: lengthMenue

Default

dt_bundle_internationalization()
mtcars %>% datatable2(
    bundles = dt_bundle_internationalization()
)

Custom options

dt_bundle_internationalization(url = testthat::test_path("data/de-DE.json"))
mtcars %>%
    datatable2(
        bundles = dt_bundle_internationalization(url = test_path("data/de-DE.json"))
    )
mtcars %>%
    datatable2(
        bundles = dt_bundle_internationalization(url = character())
    )
mtcars %>%
    datatable2(
        bundles = dt_bundle_internationalization_en()
    )
mtcars %>%
    datatable2(
        bundles = dt_bundle_internationalization_de()
    )

Available bundles


Processing bundles

dt_bundle_autofill() %>% dt_process_bundles()

list(
    dt_bundle_autofill(), 
    dt_bundle_fixedcolumns(),
    dt_bundle_buttons()
) %>% 
    dt_process_bundles()

Scratch

Problem when using buttons together with length menue https://datatables.net/faqs/index#buttons-page-length).

mtcars %>%
    datatable2(
        bundles = list(
            # dt_bundle_lengthmenue()
            dt_bundle_scroller()
            # dt_bundle_internationalization(url = test_path("data/de-DE.json"))
        )
    )
list(
    dt_bundle_lengthmenue(),
    dt_bundle_scroller(),
    dt_bundle_internationalization(url = test_path("data/de-DE.json"))
) %>% 
    dt_process_bundles()
dt_bundle_lengthmenue() %>% 
    dt_process_bundles()
mtcars %>%
    datatable2(
        bundles = list(
            dt_bundle_lengthmenue(),
            dt_bundle_internationalization(url = test_path("data/de-DE.json"))
        )
    )

Rapid prototyping/debugging

list(
    dt_bundle_buttons(),
    dt_bundle_buttons_en(),
    dt_bundle_buttons_de()
) %>% 
    dt_process_bundles()
mtcars %>%
    datatable2(
        bundles = list(
            dt_bundle_buttons(),
            dt_bundle_buttons_en(),
            dt_bundle_buttons_de()
        )
    )

Handling dubious dom

mtcars %>%
    datatable2(
        bundles = list(
            dt_bundle_scroller(),
            dt_bundle_buttons()
        )
    )
mtcars %>% 
    datatable2(
        bundles = list(
            dt_bundle_buttons(),
            dt_bundle_searchpanes()
        )
    )
mtcars %>% 
    datatable2(filter = "top")


rappster/dti documentation built on June 3, 2022, 5:10 p.m.