1 简介

此包用来给向量或数据框添加格式,以便于数据展示、丰富格式、有效传递信息。

在GitHub 和CRAN上都可以安装。

# install.packages("devtools")
devtools::install_github("renkun-ken/formattable")
install.packages("formattable")

2 formattable包应用

2.1 向量格式化

本包提供了诸如percent, comma, currency, accountingscientific等函数来给相应的数字进行格式化。

2.1.1 百分数运算

library(formattable)
p <- percent(c(0.1, 0.02, 0.03, 0.12))
p
## [1] 10.00% 2.00%  3.00%  12.00%
p + 0.05
## [1] 15.00% 7.00%  8.00%  17.00%
max(p)
## [1] 12.00%

2.1.2 计账运算

balance <- accounting(c(1000, 500, 200, -150, 0, 1200))
balance
## [1] 1,000.00 500.00   200.00   (150.00) 0.00     1,200.00
balance + 1000
## [1] 2,000.00 1,500.00 1,200.00 850.00   1,000.00 2,200.00

2.1.3 货币格式

balance <- currency((c(1000, 500, 200, -150, 0, 1200)))
balance
## [1] $1,000.00 $500.00   $200.00   $-150.00  $0.00     $1,200.00

2.1.4 大数字分隔

balance <- c(1000, 500, 200, -150, 0, 1200)* 100
balance
## [1] 100000  50000  20000 -15000      0 120000
balance <- comma((c(1000, 500, 200, -150, 0, 1200))* 100)
balance
## [1] 100,000.00 50,000.00  20,000.00  -15,000.00 0.00       120,000.00

2.1.5 科学计数

balance <- c(1000, 500, 200, -150, 0, 1200)* 100
balance
## [1] 100000  50000  20000 -15000      0 120000
balance <- scientific((c(1000, 500, 200, -150, 0, 1200))* 100)
balance
## [1] 1.0000e+05  5.0000e+04  2.0000e+04  -1.5000e+04 0.0000e+00  1.2000e+05

3 表格格式化

formattable() 函数可以为numeric, logical, factor, Date, data.frame等类型数据高度订制格式。

p <- data.frame(
  id = c(1, 2, 3, 4, 5), 
  name = c("A1", "A2", "B1", "B2", "C1"),
  balance = accounting(c(52500, 36150, 25000, 18300, 7600), format = "d"),
  growth = percent(c(0.3, 0.3, 0.1, 0.15, 0.15), format = "d"),
  ready = formattable(c(TRUE, TRUE, FALSE, FALSE, TRUE), "yes", "no"))
p
##   id name balance growth ready
## 1  1   A1  52,500    30%   yes
## 2  2   A2  36,150    30%   yes
## 3  3   B1  25,000    10%    no
## 4  4   B2  18,300    15%    no
## 5  5   C1   7,600    15%   yes

4 动态文档表格格式化

df <- data.frame(
  id = 1:10,
  name = c("Bob", "Ashley", "James", "David", "Jenny", 
    "Hans", "Leo", "John", "Emily", "Lee"), 
  age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
  grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
  test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
  test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
  final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
  registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
  stringsAsFactors = FALSE)

普通格式表格:

id name age grade test1_score test2_score final_score registered
1 Bob 28 C 8.9 9.1 9.00 TRUE
2 Ashley 27 A 9.5 9.1 9.30 FALSE
3 James 30 A 9.6 9.2 9.40 TRUE
4 David 28 C 8.9 9.1 9.00 FALSE
5 Jenny 29 B 9.1 8.9 9.00 TRUE
6 Hans 29 B 9.3 8.5 8.90 TRUE
7 Leo 27 B 9.3 9.2 9.25 TRUE
8 John 27 A 9.9 9.3 9.60 FALSE
9 Emily 31 C 8.5 9.1 8.80 FALSE
10 Lee 30 C 8.6 8.8 8.70 FALSE

将以下方式可视化表格:

library(formattable)
formattable(df, list(
  age = color_tile("white", "orange"),
  grade = formatter("span", style = x ~ ifelse(x == "A", 
    style(color = "green", font.weight = "bold"), NA)),
  area(col = c(test1_score, test2_score)) ~ normalize_bar("pink", 0.2),
  final_score = formatter("span",
    style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
    x ~ sprintf("%.2f (名次: %02d)", x, rank(-x))),
  registered = formatter("span",
    style = x ~ style(color = ifelse(x, "green", "red")),
    x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
id name age grade test1_score test2_score final_score registered
1 Bob 28 C 8.9 9.1 9.00 (名次: 06) Yes
2 Ashley 27 A 9.5 9.1 9.30 (名次: 03) No
3 James 30 A 9.6 9.2 9.40 (名次: 02) Yes
4 David 28 C 8.9 9.1 9.00 (名次: 06) No
5 Jenny 29 B 9.1 8.9 9.00 (名次: 06) Yes
6 Hans 29 B 9.3 8.5 8.90 (名次: 08) Yes
7 Leo 27 B 9.3 9.2 9.25 (名次: 04) Yes
8 John 27 A 9.9 9.3 9.60 (名次: 01) No
9 Emily 31 C 8.5 9.1 8.80 (名次: 09) No
10 Lee 30 C 8.6 8.8 8.70 (名次: 10) No

图标集由 GLYPHICONS.com and Bootstrap提供。

df <- data.frame(
  stringsAsFactors = FALSE,
              Food = c("Chicken","Salad","Beef","Fish","Rice","Pork",
                       "Turkey","Meat","Cheese","Oyster",
                       "Milk","Fruit","Egg","Potato"),
         outbreaks = c(1309L,1331L,958L,644L,406L,
                       390L,336L,295L,366L,244L,217L,171L,252L,291L),
         illnesses = c(23464L,36968L,18962L,3704L,
                       4925L,8710L,10680L,5103L,6924L,2742L,5375L,5754L,
                       6739L,9836L),
  hospitalizations = c(1058L,763L,975L,239L,152L,
                       575L,364L,214L,547L,73L,241L,98L,341L,232L),
        fatalities = c(11L,18L,18L,3L,9L,5L,21L,
                       23L,18L,2L,6L,2L,3L,7L)
)

dt <- formattable(df, list(
  outbreaks = color_bar("#b8ddf2"),
 illnesses = color_bar("#6ed46e"),
 hospitalizations = color_bar("#f7d383"),
 fatalities = color_bar("#eb724d")))
dt
Food outbreaks illnesses hospitalizations fatalities
Chicken 1309 23464 1058 11
Salad 1331 36968 763 18
Beef 958 18962 975 18
Fish 644 3704 239 3
Rice 406 4925 152 9
Pork 390 8710 575 5
Turkey 336 10680 364 21
Meat 295 5103 214 23
Cheese 366 6924 547 18
Oyster 244 2742 73 2
Milk 217 5375 241 6
Fruit 171 5754 98 2
Egg 252 6739 341 3
Potato 291 9836 232 7

5 格式集订制

可以给某批数据订制统一的格式函数。

sign_formatter <- formatter("span", 
  style = x ~ style(color = ifelse(x > 0, "green", 
    ifelse(x < 0, "red", "black"))))

above_avg_bold <- formatter("span", 
  style = x ~ style("font-weight" = ifelse(x > mean(x), "bold", NA)))

products <- data.frame(id = 1:5, 
  price = c(10, 15, 12, 8, 9),
  rating = c(5, 4, 4, 3, 4),
  market_share = percent(c(0.1, 0.12, 0.05, 0.03, 0.14)),
  revenue = accounting(c(55000, 36400, 12000, -25000, 98100)),
  profit = accounting(c(25300, 11500, -8200, -46000, 65000)))
products
##   id price rating market_share     revenue      profit
## 1  1    10      5       10.00%   55,000.00   25,300.00
## 2  2    15      4       12.00%   36,400.00   11,500.00
## 3  3    12      4        5.00%   12,000.00  (8,200.00)
## 4  4     8      3        3.00% (25,000.00) (46,000.00)
## 5  5     9      4       14.00%   98,100.00   65,000.00
products
##   id price rating market_share     revenue      profit
## 1  1    10      5       10.00%   55,000.00   25,300.00
## 2  2    15      4       12.00%   36,400.00   11,500.00
## 3  3    12      4        5.00%   12,000.00  (8,200.00)
## 4  4     8      3        3.00% (25,000.00) (46,000.00)
## 5  5     9      4       14.00%   98,100.00   65,000.00
formattable(products, list(
  price = color_tile("transparent", "lightpink"),
  rating = color_bar("lightgreen"),
  market_share = color_bar("lightblue"),
  revenue = above_avg_bold,
  profit = sign_formatter))
id price rating market_share revenue profit
1 10 5 10.00% 55,000.00 25,300.00
2 15 4 12.00% 36,400.00 11,500.00
3 12 4 5.00% 12,000.00 (8,200.00)
4 8 3 3.00% (25,000.00) (46,000.00)
5 9 4 14.00% 98,100.00 65,000.00

6 参考文献

  1. https://github.com/renkun-ken/formattable/tree/master/vignettes

  2. https://www.kaggle.com/bdetanico/summary-report-on-foodborne-disease-outbreaks