在R语言formattable包中,如何将total行设置为加粗且字号为8?
在formattable中设置Total行的加粗与字号样式
你可以通过在formatter的style参数中加入条件判断,针对category列的total值单独设置加粗和字号样式。修改后的代码如下:
library(formattable) df <- data.frame(category=c('a','b','total'),value=c(1,2,3)) formattable(df,list( category = formatter("span", style = x ~ style( color = "red", font.weight = ifelse(x == "total", "bold", "normal"), font.size = ifelse(x == "total", "8px", "inherit") ) ), # 若需同步设置value列的total行样式,可添加以下配置 value = formatter("span", style = x ~ style( font.weight = ifelse(df$category == "total", "bold", "normal"), font.size = ifelse(df$category == "total", "8px", "inherit") ) ) ))
关键说明:
- 借助
ifelse()判断当前值是否为total,对目标行应用font.weight = "bold"实现加粗,font.size = "8px"设置字号; - 非total行可设置
font.weight = "normal"(默认字重)和font.size = "inherit"(继承默认字号),也可根据需求调整为其他样式; - 若要让
value列的total行同步样式,需单独为该列配置formatter,通过df$category匹配行条件。
内容的提问来源于stack exchange,提问作者anderwyang
相关产品推荐
相关产品推荐

