rformattable

In r/formattable, how to adjust column format not include `total` row


In r/formattable, how to adjust column format not include total row ?

library(formattable)
df <- data.frame(category=c('a','b','total'),value=c(1,2,3))
formattable(df,list(
  category[-3,] = formatter("span",
                       style = x ~ style(color = "red")
  )
))

Solution

  • We may use ifelse

    library(formattable)
    formattable(df,list(
      category = formatter("span",
                                style = x ~ ifelse(x != 'total', 
           style(color = "red"), x)
      )
    ))
    

    enter image description here


    If it is for the value column based on the 'category'

    formattable(df,list(
      value = formatter("span",
                                style = x ~ ifelse(df$category != 
           'total', style(color = "red"), x)
      )
    ))