R炒面机器学习程序员

11-使用kableExtra包美化表格

2019-12-04  本文已影响0人  wonphen

安装

if (!require("kableExtra")) install.packages("kableExtra") else library("kableExtra")

表格整体设定

kable_styling()设定表格整体,它会自动应用bootstrap风格主题。
它的可选参数:
bootstrap_options设定主题。可选值有striped, bordered, hover, condensed, responsive。
full_width设定是否响应屏幕大小。TRUE和FALSE取值。full_width=F,不随着屏幕大小变化而变化。
Position设定屏幕足够大时表格位置。可选值为center, left和right。分别为居中,左偏,右偏。
Font_size设定表格内文字大小。
fixed_thead = T时,调整表头以适应屏幕。

表格操作

column_spec(kable_input, column, width = NULL, bold = FALSE,
italic = FALSE, monospace = FALSE, underline = FALSE,
strikeout = FALSE, color = NULL, background = NULL,
border_left = FALSE, border_right = FALSE, width_min = NULL,
width_max = NULL, extra_css = NULL, include_thead = FALSE)

row_spec(kable_input, row, bold = FALSE, italic = FALSE,
monospace = FALSE, underline = FALSE, strikeout = FALSE,
color = NULL, background = NULL, align = NULL, font_size = NULL,
angle = NULL, extra_css = NULL, hline_after = FALSE,
extra_latex_after = NULL)

column_spec()选中列
row_spec()选中行
row_spec(0)选中变量名
这些函数内可以添加各种参数,包括:
bold = T,加粗
strikeout =T,加删除线
monospace = T,等宽字体
underline = T,下划线
color,设定表中数据颜色
background,设定背景颜色
angle,设定角度

data <- read.csv("C:\\Users\\Admin\\Documents\\R\\data_set\\class.csv", header = T)
data %>% 
  kable() %>%
  kable_styling("responsive", full_width = F, font_size = 14, fixed_thead = T) %>% 
  column_spec(4:5, bold = T) %>%
  row_spec(c(1, 3:5, 7), bold = T, color = "blue", background = "yellow")
美化效果
上一篇下一篇

猜你喜欢

热点阅读