Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create new columns conditionally in reactable from R

I am trying to make a reactable with the following (I succeeded making the first but not 2 and 3):

  1. If a row in any column is above 5 or below 0.1, color that cell red.
  2. Create a new column that counts the number of red cells for every row. For instance, since the first row has two red cells, the value is 2.
  3. Create another column that is either 1 or 0: 1 if all four columns in a certain row are all white and 0 otherwise. For instance, since the 7th row does not have any red cells, the value will be 1.
    enter image description here
library(tidyverse)
library(reactable)

reactable(
iris,
columns = set_names(x = colnames(iris)) %>% 
    map(~ {
    colDef(
        style = function(value) {
        ds_color <- ifelse(value > 5 | value < 0.1, "red", "white")
        list(background = ds_color)
        }
    )
    })
)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

We may use if_all

library(dplyr)
iris %>%
   mutate(color_ind = case_when(if_all(where(is.numeric), ~ 
     .x > 5|.x < 1)~ "red",TRUE ~  "white"),
   cnt_col = rowSums(across(Sepal.Length:Petal.Width, 
       ~ .x > 5|.x < 1), na.rm = TRUE),
    binary_white = +(!cnt_col))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading