How to Apply `formatStyle` on Multiple Colums on datatable

Viewed 439

I'm trying to apply style on multiple columns on a datatable using DT package:

For example, I want to create color bars for each column based on that column's range. I already figured out I can do a for loop:

columns <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")

dt <- DT::datatable(iris)
for( x in columns){
  dt <- dt %>%
    DT::formatStyle(
      x,
      background = DT::styleColorBar(iris[[x]],"steelblue"),
      backgroundSize = '90% 90%',
      backgroundRepeat = 'no-repeat',
      backgroundPosition = 'center'
    )

}
dt

enter image description here

But,personally I'm not a big fun of loops, is there a built-in or simpler way to "map" formatStyle without using loops? Thanks!


I already checked the DT guide on https://rstudio.github.io/DT/010-style.html , the last example looks similar. But in the example, different columns are sharing the same range. What I want is that each column uses its own min and max to create color bars.

0 Answers
Related