Search code examples
rif-statementformattable

R: Formattable -Column Will Not Update Based Upon Condition


I run this code to produce the formatted table and I get no error but the condition I have set in place appears to not be working at all. I It looks right? What I am trying to do is for 'mAge' column if the value in that row is > 45.63, then the color should be red. If it is not that color then no change.

formattable(data.4)
formattable(data.4, align = c("l", rep("r", NCOL(data.4) - 1)))
formattable(data.4,list(`ICD10Code` = formatter(
"span", style = ~ style(color = "black",font.weight = "bold"))),`mAge` = formatter(
"span", style = ~ ifelse("mAge" > 45.63, style(color = "red", font.weight = "bold"), NA)))

Data Sample:

ICD10Code mAge Frequency
         50.6         5
  A084   35.0         1
  A609   31.0         1
  A749   38.0         1
  B001   37.0         1
  B079   47.0         1

enter image description here Clearly, I have something off but I am not sure what as I get no errors.


Solution

  • I think it is parentheses placement (and removing quotes from mAge in ifelse):

    library(formattable)
    
    formattable(data.4)
    formattable(data.4, align = c("l", rep("r", NCOL(data.4) - 1)))
    formattable(data.4,list(
      `ICD10Code` = formatter(
        "span", style = ~ style(color = "black", font.weight = "bold")),
      `mAge` = formatter(
        "span", style = ~ ifelse(mAge > 45.63, style(color = "red", font.weight = "bold"), NA))))