Display long table in Rmarkdown

Viewed 1015

Is there a way to display a table with many columns nicely in rmarkdown PDF output? looking for some wrapper option to display it as say 3 consecutive tables but without breaking the data frame into 3 separate frames. Here is my chunk that makes the table very small and barely legible.

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'))
2 Answers

I don't know how to use kable. I prefer to use xtable and using LaTeX code.

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=" ")))

The code is a bit long but it should work

enter image description here

For what it worth, I broke the dat into 3 parts and kabled all separately:

 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'))
Related