Search code examples
rformattable

Set font size to header's elements after applied a fomatter to multiple columns in formattable


With code below, I'm able to customize column mpg and disp's format:

library(formattable)
data(mtcars)

df <- mtcars

f1 <- formatter("span", style = x ~ style(color = ifelse(x > 0, red, blue), "font-size:20px"))
f2 <- formatter("span", style = x ~ style("font-size:20px"))

formattable(df, list(
  `mpg` = f1,
  `disp` = f2
  ))

enter image description here Based what I have done above, I would like to set font size for header's elements by applying f2 to header:

formattable(df, list(
  `mpg` = f1,
  `disp` = f2,
  names(df) <- f2(names(df))
  ))

But as you may notice, the effect for mpg and disp have disappeared, any ideas to deal with this issue? Thanks.

enter image description here


Solution

  • Problem solved by the code below:

    library(formattable)
    data(mtcars)
    
    df <- mtcars
    
    f1 <- formatter("span", style = x ~ style(color = ifelse(x > 0, red, blue), "font-size:20px"))
    f2 <- formatter("span", style = x ~ style("font-size:20px"))
    
    df$mpg <- f1(df$mpg) 
    df$disp <- f2(df$disp) 
    names(df) <- f2(names(df))
    formattable(df)
    

    Out:

    enter image description here