Search code examples
rtablehtml

Conditional highlighting of rows in tableHTML


In tableHTML: Is there a way to highlight complete rows based on one conditional column? Something like this, but also with red cells in the mpg-, cyl- and disp-columns:

tableHTML(mtcars[1:10,1:3]) %>%
add_css_conditional_column(conditional = "contains", 
                           value = "Hornet",
                           css = list('background-color', "red"), 
                           columns = "rownames")

Solution

  • In the recently published version of tableHTML (version 2.1.0), there's an option to choose logical as a conditional type with add_css_conditional_column, which can be also used as a proxy for row conditional highlighting if it was applied to all columns

    Basically you define the logical vector for the condition (just like the answer by clemens), and choose to apply it to all columns like so:

    my_df <- mtcars[1:10,1:3]
    conditional <- grepl("Hornet", rownames(my_df)) 
    
    tableHTML(my_df) %>%
      add_css_conditional_column(conditional = "logical", 
                                 columns = 0:ncol(my_df),
                                 css = list('background-color', "red"), 
                                 logical_conditions = list(conditional))
    

    enter image description here