为 Rmarkdown pdf 自定义 table

custom table for Rmarkdown pdf

我有一个 table 如下(dput 在最后)

# A tibble: 35 x 5
# Groups:   year [1]
   school_id class_id  year variable                value
       <dbl> <chr>    <dbl> <chr>                   <dbl>
 1       123 1         2020 sex_numerator_male        137
 2       123 1         2020 sex_numerator_female       67
 3       123 1         2020 sex_denominator_male   156986
 4       123 1         2020 sex_denominator_female 173055
 5       123 1         2020 age_numerator_18_34        76
 6       123 1         2020 age_numerator_35_54        97
 7       123 1         2020 age_numerator_55           31
 8       123 1         2020 age_denominator_18_34   98662
 9       123 1         2020 age_denominator_35_54  101261
10       123 1         2020 age_denominator_55     130118

我想做的是自定义 table 如下所示。我无法在 R 中解决它,因此粘贴了一个屏幕截图。应显示变量名称的相应值。为了便于理解,只显示table图中的变量名。

我正在使用 GT 包将数据组织成所需的 table。无法成功。除了性别、年龄,我还有其他行值。我正在尝试的是

  1. 控制 variable 列及其相应 values 的放置位置,格式类似于上面的示例 table 快照。
  2. 我将 table 按 3 列分组,School_id, class_id and year。因此,我可以分别为每个组生成 table。

如果有任何解决方案建议,我们将不胜感激。

structure(list(school_id = c(123, 123, 123, 123, 123, 123, 123, 
123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 
123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 
123, 123), class_id = c("1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1"), year = c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020), variable = c("sex_numerator_male", 
"sex_numerator_female", "sex_denominator_male", "sex_denominator_female", 
"age_numerator_18_34", "age_numerator_35_54", "age_numerator_55", 
"age_denominator_18_34", "age_denominator_35_54", "age_denominator_55", 
"all_sex_numerator_male_sum", "all_sex_denominator_male_sum", 
"all_sex_numerator_female_sum", "all_sex_denominator_female_sum", 
"all_age_numerator_18_34_sum", "all_age_denominator_18_34_sum", 
"all_age_numerator_35_54_sum", "all_age_denominator_35_54_sum", 
"all_age_numerator_55_sum", "all_age_denominator_55_sum", "total_num_sex_m", 
"total_num_sex_f", "total_num_age_18_34", "total_num_age_35_54", 
"total_num_age_55", "county_OD_death_sex_m", "county_OD_death_sex_f", 
"county_OD_death_age_18_34", "county_OD_death_age_35_54", "county_OD_death_age_55", 
"allhcs_OD_death_sex_m", "allhcs_OD_death_sex_f", "allhcs_OD_death_age_18_34", 
"allhcs_OD_death_age_35_54", "allhcs_OD_death_age_55"), value = c(137, 
67, 156986, 173055, 76, 97, 31, 98662, 101261, 130118, 1441, 
1908231, NA, 2073091, 687, 1237334, 996, 1228864, NA, 1515124, 
47.5656054853791, 52.4343945146209, 29.8938616717317, 30.6813395911417, 
39.4247987371266, 87.2689284394787, 38.7160151396955, 77.0306703695445, 
95.7920620969574, 23.8245285048956, 75.5149664794252, NA, 55.5225993951512, 
81.0504661215562, NA)), row.names = c(NA, -35L), groups = structure(list(
    year = 2020, .rows = structure(list(1:35), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

这是您可以构建的示例:

library(dplyr)
library(kableExtra)

collapse_rows_dt <- data.frame(
  C1 = c(rep("Sex", 2), rep("Age", 5)),
  C2 = c("m", "f", "18-34", "35-44", "45-54", "55-64", "65+"),
  C3 = 1:7,
  C4 = sample(0:100, 7, replace = TRUE))

names(collapse_rows_dt) <- c("", "", "Total Number", "County")

kbl(collapse_rows_dt, align = "c") %>%
  # set background on first two columns
  column_spec(1:2, bold = T, background = "#999", width = "4em") %>% 
  # set background and font color on header row
  row_spec(0, bold = T, color = "#000", background = "#999") %>% 
  # add your header
  add_header_above(c("Class 1: Results for 2020" = 4)) %>% 
  # collapse the rows so they look "stacked"
  collapse_rows(columns = 1:2, valign = "top") %>% 
  # set a theme
  kable_classic(full_width = T)

这应该足以让您入门。一个问题是 kableExtra 不支持 HTML 中的垂直边框,仅支持 LaTeX。但是如果你能避免使用 LaTeX,你会更高兴。 kableExtra 调用的每一行都用注释分隔,该注释标识下一行的作用。 kableExtra 中有更多选项。

这是一个小插图,应该有你需要的一切:

https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html#Column__Row_Specification

PS:避免 LaTeX

PPS:避免使用 LaTeX!