Search code examples
htmlrformattable

change header style formattable R


I'm trying to use formattable with some values for species, thus, it's very important for column names to be italic; I've tried with the formatter() function, but it only acts on the values, even if I use the "th" node instead of "span"

library(formattable)
make_italic <- formatter("span",
                      style =  "font-style:italic")
formattable(mtcars, list(mpg = make_italic, qsec = make_italic))

In the mtcars, how may I change the names (mpg, cyl, disp,...) to italic?


Solution

  • I don't know the formattable package, but the make_italic object that you create is a function that adds italics tags to character objects. You can use that on the column names directly. Because the names get changed you can no longer use them in your formattable function to format the columns, however you can format those column in the data.frame before changing the column names the same way. A bit hackish, but works.

    library(formattable)
    data(mtcars)
    mtcars_tab        <- mtcars 
    make_italic       <- formatter("span", style =  "font-style:italic")
    mtcars_tab$mpg    <- make_italic(mtcars_tab$mpg)
    mtcars_tab$qsec   <- make_italic(mtcars_tab$qsec)
    names(mtcars_tab) <- make_italic(names(mtcars_tab))
    formattable(mtcars_tab)