Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling R styling via a new param formatter #949

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft

Enabling R styling via a new param formatter #949

wants to merge 23 commits into from

Conversation

shrektan
Copy link
Collaborator

@shrektan shrektan commented Dec 5, 2021

Closes #938 #652 #990

DT implements many nice JS wrappers on styling the columns. However, R users may still find they want to implement their own style, as the love for beautiful table never ends. They can do it in two ways:

  1. On JS side: Write a JS function and set it in render options, i.e., datatable(..., options = list( columnDefs = list( list(targets = 1, render = JS(...)))))
  2. On R side: Transform the R data.frame object's relevent column into characters, then pass it to DT, probably need to set the escape argument to FALSE.

Unfortunately, those two methods are either difficult to implement or contain side-effects:

  1. JS method:
  • Many R users are not aware of the existence of the columnDefs.render option
  • Write the JS function correctly is not easy, especially when the "style" is complicated
  • You can't leverage the existing R packages like formattable
  1. R method:
  • Probably lose the ability to sort, filter or search correctly as the raw "data" is tranformed into formatted "data"
  • Probably need additional efforts to "unescape" data, align text, etc.

The new param formatter allows users to write formatting functions in R without losing the ability to sort, filter or search by retaining the raw data "untouched" as it creates invisible helper columns.

TODO

  • support formatter type like list(function() ... , targets = ...)
  • figure out a way when the formatter need other info from the data, like other column
  • try out all the possible corner cases in case of unexpected happening
  • state clear the limitations in doc (probably cause issues when editing data
  • need to triger updating the hidden formatted columns when source is updated, e.g, via proxy, or editing
  • NEWS
  • Add an example into inst/example
  • Add an example into site
  • add tests

Example

set.seed(100)
x <- rnorm(10)
rk <- rank(x)
prefix <- sample(LETTERS[1:3], replace = TRUE, 10)
tbl <- data.frame(V1 = x, V2 = x, V3 = x, V4 = x, RK = rk)
DT::datatable(tbl, rownames = FALSE, filter = "top", formatter = list(
  V2 = function(x) paste0(prefix, round(x * 100, 2), "%"),
  V3 = formattable::percent,
  V4 = function(x) round(x * 100, 2),
  V4 = formattable::color_tile(min.color = "yellow", max.color = "red")
))

image

@shrektan shrektan marked this pull request as draft December 5, 2021 15:55
@shrektan
Copy link
Collaborator Author

shrektan commented Jan 20, 2022

Now I think this feature is not suitable for DT as it becomes more complicated than I thought. I don't wanna introduce any complexity into DT as it's already a big project.

Close it for now.

@shrektan shrektan closed this Jan 20, 2022
@philibe
Copy link

philibe commented Jan 20, 2022

It's was a very good try. Thanks. (Sob). Perhaps later ? :)

Merge remote-tracking branch 'origin/main' into styling

# Conflicts:
#	DESCRIPTION
#	NEWS.md
#	R/datatables.R
#	man/datatable.Rd
#	tests/testit/test-datatables.R
@shrektan shrektan reopened this Apr 7, 2022
@philibe
Copy link

philibe commented Apr 8, 2022

@yihui , @shrektan

This reopened issue gives me the opportunity to thank you for your work on DT.

When I first installed DT in early 2018, for me it was a simple adaptation of datatable.net, but now that I'm trying to use datatable.net with Flask Rest and VueJs 3 I see that I have to rewrite a lot of things which I thought was in datatable.net but actually written in the server side of DT, and I understand how your API wrapping initial datatable.net ones in the front side.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scientific notation format function for DT
3 participants