R package DT - how to highlight the maximum in a row

Using the DT package in R, I would like to allocate the max / min of each row for a numeric data frame.

Suppose we have built a segmentation with 6 clusters, and then we want to describe these clusters using descriptive variables:

library(DT)
library(tidyverse)
df <- iris[1:6, 1:4] %>% t()
(df)

#                1   2   3   4   5   6
# Sepal.Length 5.1 4.9 4.7 4.6 5.0 5.4
# Sepal.Width  3.5 3.0 3.2 3.1 3.6 3.9
# Petal.Length 1.4 1.4 1.3 1.5 1.4 1.7
# Petal.Width  0.2 0.2 0.2 0.2 0.2 0.4

      

We would like to visualize which clusters have the highest Sepal.Length, which have the highest Sepal.Width, etc. The solution will be the color maximum of each line.

If I use the formatStyle () function of the DT package, I can do it column by column (highlight the maximum, color values ​​according to specific intervals, ...), but I cannot do it for each row. Even if I use the target = 'row' parameter, the whole row will be colored, not just the maximum or cells that I would like.

df %>% datatable() %>% 
   formatStyle('1', background = styleEqual(max(df[,1]), 'green'))

      

An alternative would be to translate the row and columns of my dataframe, but I prefer to have a dataframe with 50 rows and 5 columns instead of one with 5 rows and 50 columns with very long names.

Thanks in advance for your help and advice.

+3


source to share


1 answer


You need a custom function for this rowCallback

. Here's an example:

iris[1:6, 1:4] %>% datatable(options=list(rowCallback = JS(
  'function(row, data) {
    var num_data = data.slice(1,data.length)
    var row_max = Math.max.apply(Math,num_data);
    var row_min = Math.min.apply(Math,num_data);
    for(i=1;i < data.length; i++) {
      if(data[i]==row_max) {
        $("td:eq("+i+")", row).css("background-color", "green")
      } else if(data[i]==row_min) {
        $("td:eq("+i+")", row).css("background-color", "yellow")
      }
    }
  }')))

      

Note that you need to filter out any text columns from num_data

, including the first one that has height names.



If you want to highlight the second highest value, you can sort num_data

and adapt the if / else in the JS code to color whatever you want. After sorting, num_data

num_data[num_data.length-1]

is the maximum, num_data[0]

is the min, num_data [num_data.length-2] `is the second maximum, and so on.

iris[1:6, 1:4] %>% datatable(options=list(rowCallback = JS(
  'function(row, data) {
    var num_data = data.slice(1,data.length)
    num_data.sort(function (a, b) {  return a - b;  });
    for(i=1;i < data.length; i++) {
    if(data[i]==num_data[num_data.length-1]) {
      $("td:eq("+i+")", row).css("background-color", "green")
    } else if(data[i]==num_data[0]) {
      $("td:eq("+i+")", row).css("background-color", "yellow")
    } else if(data[i]==num_data[num_data.length-2]) {
      $("td:eq("+i+")", row).css("background-color", "orange")
    }
    }
  }')))

      

+2


source







All Articles