change header style formattable R

纵然是瞬间 提交于 2019-12-11 10:06:56

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/52783329/change-header-style-formattable-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!