第 1 章 表格

这是一个稍微复杂一点的经典表格样式,常出现在论文或期刊的数值模拟比较部分1

library(kableExtra)
db <- mtcars[, 1:7] %>% 
  transform(Methods = rownames(.)) %>% 
  `colnames<-`(., value = c(rep(c("Bias", "RMSE"), 3), "", "Methods"))
kable(db,
  format = "html", booktabs = TRUE, escape = T, row.names = F,
  caption = "第1种类型的统计表格样式"
) %>%
  kable_styling(
    bootstrap_options = c("basic"),
    full_width = F, position = "center"
  ) %>%
  add_header_above(c(
    "$\\sigma^2$" = 2, "$\\phi$" = 2,
    "$\\tau^2$" = 2, "$r=\\delta/\\phi$" = 1, ""
  ), escape = F) %>%
  footnote(
    general_title = "注:", title_format = "italic", threeparttable = T,
    general = "* 星号表示的内容可以很长"
  )
表 1.1: 第1种类型的统计表格样式
\(\sigma^2\)
\(\phi\)
\(\tau^2\)
\(r=\delta/\phi\)
Bias RMSE Bias RMSE Bias RMSE Methods
21.0 6 160.0 110 3.90 2.620 16.46 Mazda RX4
21.0 6 160.0 110 3.90 2.875 17.02 Mazda RX4 Wag
22.8 4 108.0 93 3.85 2.320 18.61 Datsun 710
21.4 6 258.0 110 3.08 3.215 19.44 Hornet 4 Drive
18.7 8 360.0 175 3.15 3.440 17.02 Hornet Sportabout
18.1 6 225.0 105 2.76 3.460 20.22 Valiant
14.3 8 360.0 245 3.21 3.570 15.84 Duster 360
24.4 4 146.7 62 3.69 3.190 20.00 Merc 240D
22.8 4 140.8 95 3.92 3.150 22.90 Merc 230
19.2 6 167.6 123 3.92 3.440 18.30 Merc 280
17.8 6 167.6 123 3.92 3.440 18.90 Merc 280C
16.4 8 275.8 180 3.07 4.070 17.40 Merc 450SE
17.3 8 275.8 180 3.07 3.730 17.60 Merc 450SL
15.2 8 275.8 180 3.07 3.780 18.00 Merc 450SLC
10.4 8 472.0 205 2.93 5.250 17.98 Cadillac Fleetwood
10.4 8 460.0 215 3.00 5.424 17.82 Lincoln Continental
14.7 8 440.0 230 3.23 5.345 17.42 Chrysler Imperial
32.4 4 78.7 66 4.08 2.200 19.47 Fiat 128
30.4 4 75.7 52 4.93 1.615 18.52 Honda Civic
33.9 4 71.1 65 4.22 1.835 19.90 Toyota Corolla
21.5 4 120.1 97 3.70 2.465 20.01 Toyota Corona
15.5 8 318.0 150 2.76 3.520 16.87 Dodge Challenger
15.2 8 304.0 150 3.15 3.435 17.30 AMC Javelin
13.3 8 350.0 245 3.73 3.840 15.41 Camaro Z28
19.2 8 400.0 175 3.08 3.845 17.05 Pontiac Firebird
27.3 4 79.0 66 4.08 1.935 18.90 Fiat X1-9
26.0 4 120.3 91 4.43 2.140 16.70 Porsche 914-2
30.4 4 95.1 113 3.77 1.513 16.90 Lotus Europa
15.8 8 351.0 264 4.22 3.170 14.50 Ford Pantera L
19.7 6 145.0 175 3.62 2.770 15.50 Ferrari Dino
15.0 8 301.0 335 3.54 3.570 14.60 Maserati Bora
21.4 4 121.0 109 4.11 2.780 18.60 Volvo 142E
注:
* 星号表示的内容可以很长
  • striped 默认使用浅灰色,stripe_color 可以用来指定颜色 stripe_color="white",它只在 LaTeX 下工作,HTML 下更改颜色需要设置 CSS,可以不使用 striped 改变默认的白底设置
  • threeparttable = TRUE 处理超长的注解标记,
  • add_header_above 函数内的 escape = F 用来处理数学公式,
  • longtable = T 表格很长时需要分页,因此使用续表,
  • hold_position 使用了 [!h] 控制浮动
  • 对于数学符号前要四个反斜杠这一点,作者今后可能会改变,只需要两个反斜杠,与 HTML 格式表格保持一致 https://github.com/haozhu233/kableExtra/issues/120
  • 对某些数据用不同颜色高亮 Selecting and colouring single table cells with kableExtra in R markdown cell_spec https://stackoverflow.com/questions/50118394

1.1 Pandoc 支持的表格

其实 Pandoc’s Markdown 本身也支持不少表格样式2,比如常见的简单表格样式,如表 1.2 和表 1.3

表 1.2: 简单表格语法展示
Right Left Center Default
12 12 12 12
123 123 123 123
1 1 1 1

再来一个表格

表 1.3: 表格标题可以含有脚注3
First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

1.2 软件信息

sessionInfo(.packages(T))
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.6 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/openblas-base/libblas.so.3
## LAPACK: /usr/lib/libopenblasp-r0.2.18.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
##  [1] base      compiler  datasets  graphics  grDevices grid      methods  
##  [8] parallel  splines   stats     stats4    tcltk     tools     utils    
## 
## other attached packages:
##  [1] askpass_1.1        base64enc_0.1-3    bookdown_0.20      callr_3.4.4       
##  [5] colorspace_1.4-1   curl_4.3           digest_0.6.25      evaluate_0.14     
##  [9] farver_2.0.3       glue_1.4.2         highr_0.8          htmltools_0.5.0   
## [13] httr_1.4.2         jsonlite_1.7.1     kableExtra_1.2.1   knitr_1.30        
## [17] labeling_0.3       lifecycle_0.2.0    magrittr_1.5       markdown_1.1      
## [21] mime_0.9           munsell_0.5.0      openssl_1.4.3      processx_3.4.4    
## [25] ps_1.3.4           R6_2.4.1           RColorBrewer_1.1-2 remotes_2.2.0     
## [29] rlang_0.4.7        rmarkdown_2.3      rstudioapi_0.11    rvest_0.3.6       
## [33] scales_1.1.1       selectr_0.4-2      stringi_1.5.3      stringr_1.4.0     
## [37] sys_3.4            tinytex_0.26       viridisLite_0.3.0  webshot_0.5.2     
## [41] xfun_0.17          xml2_1.3.2         yaml_2.2.1         boot_1.3-25       
## [45] class_7.3-17       cluster_2.1.0      codetools_0.2-16   foreign_0.8-80    
## [49] KernSmooth_2.23-17 lattice_0.20-41    MASS_7.3-51.6      Matrix_1.2-18     
## [53] mgcv_1.8-31        nlme_3.1-148       nnet_7.3-14        rpart_4.1-15      
## [57] spatial_7.3-12     survival_3.1-12