如何使用R formattable包更改表标题行的外观 [英] How to change appearance of table header row with R formattable package

查看:493
本文介绍了如何使用R formattable包更改表标题行的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R中的formattable包来生成HTML表。我可以使用formatter自定义表格中的数据值外观,例如字体大小,颜色等。但我无法弄清楚如何改变表头行的外观。我可以使用col.names()更改实际的列名称,但无法更改其外观。



例如,在下表中,如何更改标题行中的文本颜色或背景颜色(mpg,cyl,disp等)



最终,我计划使用formattable :: as.htmlwidget()和库(webshot)来获取表格的图像文件,参见
使用Formattable包在R中导出/保存表的命令 / p>

谢谢

  library(formattable)

formatRed< - formatter(span
,style = x~style(color = ifelse(x> 21,red,black)))

formatSize< ; - formatter(span
,style = x~style(font-size=8px))

exTb< - format table(head(mtcars,5)
,table.attr =class ='table table-striped'
,list(mpg = formatRed
,wt = formatSize)


exTb


解决方案

我过去3天一直在调查这个问题。不幸的是,使用formattable的例子并不多,所以对于那些开始使用formattable的人来说,这有点棘手。但是,我找到了一个解决方案(感谢一些天才的帮助!)来改变标题和行的字体(你可以应用于你的问题)。诀窍是制作一个自定义CSS文件,它允许您更改表的各种元素。然后,您可以使用table.attr参数将formattable链接到此CSS文件,或使用CSS容器。这是我的问题如何更改R中格式表中的字体系列?。希望这会有所帮助!


I'm using the formattable package in R to produce a HTML table. I can use formatter to customise the look of data values in in my table e.g. font-size, color etc. But I can't work out how to alter the appearance of the table header row.I can alter the actual column names using col.names(), but haven't been able to change their appearance.

For example, in the table below how can I change the text color or background color in the header row (mpg, cyl, disp etc.)

Ultimately, I plan to use formattable::as.htmlwidget() and library(webshot) to grab an image file of the table, see Command for exporting/saving table made with Formattable package in R

Thanks

library(formattable)

formatRed <- formatter("span"
    , style = x ~ style(color = ifelse(x > 21 , "red", "black")))

formatSize <-  formatter("span"
    , style = x ~ style("font-size" = "8px"))

exTb <- formattable(head(mtcars, 5)
    , table.attr = "class='table table-striped'"
    , list(mpg = formatRed
        , wt = formatSize)
)

exTb

解决方案

I've spent the past 3 days investigating this issue. Unfortunately, there aren't many examples using formattable available, so for those starting to use formattable it is a little tricky. However, I have found a solution (thanks to the help of some geniuses!) to changing the font of the header and rows (which you may be able to apply to your problem). The trick was to make a custom CSS file, which allows you to change various elements of the table. You could then link the formattable using the table.attr parameter to this CSS file, or use a CSS container. This is my question here How do you change the font family in a formattable in R?. Hopefully this will help!

这篇关于如何使用R formattable包更改表标题行的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆