ukgovdatascience / govdown

GOV.UK Design System theme for R Markdown
https://ukgovdatascience.github.io/govdown/
Other
46 stars 5 forks source link

Add govuk_DT() features #63

Open mattkerlogue opened 4 years ago

mattkerlogue commented 4 years ago

Have recently experimented with applying gov.uk style guidelines to interactive {DT} tables in {govdown}. This is an issue to discuss development ahead of a future PR.

mattkerlogue commented 4 years ago

Source code for the experiment: https://github.com/co-analysis/people-survey-explorer/blob/master/orgscores.Rmd

mattkerlogue commented 4 years ago

THOUGHT: There should be a single user facing function govuk_DT() (or some other sensible name).

Should this function:

  1. Render a data.frame passed to it? Or,
  2. Apply styling to a DT::datable() object already defined by the user

(1) gives full control over the rendering and styling of the table, also allows the function to generate filtering controls, likely limits extent of using advanced {DT} functionality (may not be a bad thing). Main thing I'm thinking of here is using the DT::format_*() functions that allow styling (e.g. percentages), and specifiying options such as column visibility/column names. Formatting can be acheived by using {formattable} functions on the source dataset.

(2) gives greater controls over the design and structure of the table, but potential to cause design conflicts/javascript errors if the table includes unexpected elements.

nacnudus commented 4 years ago

Can DT apply formatting in layers?

mattkerlogue commented 4 years ago

With {DT} you create a datatables object DT::datatable() which you can then pipe to formatters, which can format the number (e.g. percent, currency) or the style.

Details here, with some very shocking formatting decisions.

From experience you can apply {formattable} to columns in your df e.g.

iris2 <- iris %>% mutate_at(Sepal.Length, formattable::percent, digits = 1)

And then supply this to your DT call, and it'll still show up as a percentage

DT::datatable(iris2)
nacnudus commented 4 years ago

I think a pipeable formatter would be the best approach.

iris %>%
  mutate(x = scales::percent(x)) %>%
  DT::datatable() %>%
  govdown::govdownify() %>%
  more_formatting() # User takes responsibility for conflicts with govdown from here on

As a rule I'm not in favour of using DT or similar to change the values in a table. I think that should be up to scales::percent and glue and whathaveyou.