在Rmarkdown中显示长表

lskq00tm  于 2023-04-18  发布在  其他
关注(0)|答案(3)|浏览(131)

有没有一种方法可以在rmarkdown PDF输出中很好地显示一个有很多列的表格?寻找一些 Package 选项来显示它,比如说3个连续的表格,但不将数据框分成3个单独的框架。这里是我的块,使表格非常小,几乎难以辨认。

library(knitr)
library(readr)
library(kableExtra)
dat_url <- 'https://gender-pay-gap.service.gov.uk/viewing/download-data/2019'
dat <- read_csv(dat_url) 
kable(head(dat), caption='Sample Data: 6 rows', booktabs=TRUE, linesep="")  %>%
kable_styling(latex_options =c('striped', 'scale_down'))
yc0p9oo0

yc0p9oo01#

'longtable = true'可以跨页拆分表,“repeat_header”可以,你猜对了。

x_df %>% 
   kbl(caption = "caption", 
    align = "l",
    format = "latex",
    centering = FALSE,
    booktabs = TRUE,
    longtable = TRUE,
    linesep = "") %>% 
   kable_styling(bootstrap_options = c("striped", "hover"), position = "left", full_width = F, fixed_thead = T, latex_options = c("striped", "HOLD_position", "repeat_header"))
bttbmeg0

bttbmeg02#

为了它的价值,我把dat分成3个部分,kable d都分开:

kable(head(dat[,1:7]), caption='Original Data: first 6 rows', booktabs=TRUE, linesep="")  %>%
  kable_styling(latex_options =c('striped', 'scale_down', 'hold_position'))

  kable(head(dat[,8:18]), booktabs=TRUE, linesep="")  %>%
  kable_styling(latex_options =c('striped', 'scale_down', 'hold_position'))
  
  kable(head(dat[,19:22]), booktabs=TRUE, linesep="")  %>%
  kable_styling(latex_options =c('striped', 'scale_down', 'hold_position'))
3okqufwl

3okqufwl3#

我不知道如何使用kable。我更喜欢使用xtable和使用LaTeX代码。

library(xtable)
print(xtable::xtable(dat[1:30,c(1,3)], align="ll|c"), 
      hline.after = F,
      include.rownames=F, 
      include.colnames = F,
      tabular.environment = 'longtable',
      caption.width = "8.5in",
      comment=F,
      floating = F,
      add.to.row = list(pos = list(0),
                        command =
                          paste("\\caption{ZzZzZzZz} \n \\label{tab:tb1} \n \\\\ \\hline \\hline \n",
                                " \\textbf{Employer Name} & \\textbf{Sic Codes}\\\\ \\hline \n",  
                      "\\endhead \n", 
                                " \\multicolumn{2}{r}{\\footnotesize \\emph{Continued on next page...}} \n \\\\                    
                                 \\endfoot \\hline \\hline
                                 \\endlastfoot \n",collapse=" ")))

代码有点长,但应该可以工作

相关问题