Filtering with crosstalk

This is a test of an interactive RMarkdown document within the govuk-hugo theme, on 2022-04-07 using {crosstalk} to interactively filter data.

Prepare data

{crosstalk} is an R package that supports interactive Rmarkdown files, it is particularly useful for govukhugo as it can support client-side data processing and therefore enables complex interactivity without relying on Shiny. To use {crosstalk} we initiate a SharedData object by a call to crosstalk::SharedData$new().

Compatability issues means this only works with tables (via govukhgo::govuk_datatable()) and not yet with interactive plots (via {plotly}), it has not yet been tested with interactive maps (via {leaflet}).

Filtering the table

Once enabled we can use {crosstalk} to filter the data object, and to link tables and plots. At present only the filter_select() and filter_checkbo() filters are supported for use in govukhugo.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fs_species <- crosstalk::filter_select(
  id = "filter-species",
  label = "Penguin species",
  sharedData = penguins_sd,
  group = ~species,
  multiple = FALSE
)

fs_island <- crosstalk::filter_select(
  id = "filter-island",
  label = "Island",
  sharedData = penguins_sd,
  group = ~island,
  multiple = TRUE
)

fs_sex <- crosstalk::filter_checkbox(
  id = "filter-sex",
  label = "Sex",
  sharedData = penguins_sd,
  group = ~sex,
  columns = 1
)

{crosstalk} is designed to work with the Bootstrap CSS framework which does not work well with the GOV.UK Design System, therefore the filters should be wrapped inside a call to govukhugo::unstrap() to remove the Bootstrap dependencies.

1
2
3
govukhugo::unstrap(fs_species)
govukhugo::unstrap(fs_island)
govukhugo::unstrap(fs_sex)

Filters

Contents