bogenton
bogenton

Reputation: 319

How to stop kable from repeating one row to the end of the page

As the title suggests, I wrote an Rmarkdown file to print a table, and for a reason I cannot understand, it's taking the first row of the table and repeating it until the end of the page is reached:

messed up table

It seems like the table is somehow being set to a fixed width, but I'm not sure why that is and I'm also not sure why only the one row is repeating. I would like it to look normal. The rest of the page not covered by the table can just be blank. Thanks for your help.

I have tried messing around with the full_width parameter in kable_styling, this didn't make a difference.

Here's a reproducible example. This is the header:

---
title: ""
always_allow_html: yes
output:
  pdf_document:
    latex_engine: xelatex
geometry: margin=0.3in
header-includes:
- \usepackage[T1]{fontenc}
- \usepackage{array}
- \usepackage{booktabs}
- \usepackage{xcolor}
- \usepackage{makecell}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \setmainfont{Helvetica}
- \pagenumbering{gobble}
- \DeclareTextCommand{\nobreakspace}{TU}{\leavevmode\nobreak\ }
documentclass: article
classoption: a4paper
---

And here's an R chunk that reproduces the issue, note I used setting {r, echo=FALSE, message = FALSE, warning = FALSE}.


# Packages
suppressMessages(library(extrafont))
suppressMessages(library(dplyr))
suppressMessages(library(pander))
suppressMessages(library(kableExtra))

# Data
table_info <- data.frame(a=1:4, b=1:4, c=1:4, d=1:4, e=1:4, f=1:4, g=1:4, h=1:4, i=1:4)

nrow_table <- nrow(table_info)
ncol_table <- ncol(table_info)

column_ems <- c("2em", rep("1.125em", ncol_table - 1))

fsize <- 10

  ktable <- table_info %>%
    mutate_all(linebreak) %>%
    kable(escape = FALSE, align = paste0(rep('l', ncol_table), collapse=""),
          col.names = NULL,
          booktabs = TRUE, format = "latex" , linesep = "") %>%
    row_spec(1:nrow_table, color = "darkgray") %>%
    column_spec(1, column_ems[1]) %>%
    column_spec(2, column_ems[2]) %>%
    column_spec(3, column_ems[3]) %>%
    column_spec(4, column_ems[4]) %>%
    column_spec(5, column_ems[5]) %>%
    column_spec(6, column_ems[6]) %>%
    column_spec(7, column_ems[7]) %>%
    column_spec(8, column_ems[8]) %>%
    column_spec(9, column_ems[9]) %>%
    kable_styling(font_size = fsize) %>%
    row_spec(1, align = 'c') %>%
    row_spec(2, font_size = fsize - 2)


ktable <- ktable %>% row_spec(1:(nrow_table-1), hline_after = TRUE)


ktable <- ktable %>% add_header_above(c(" " = 1, "Manganese" = 2,
                                        "Phosphorus" = 2,
                                        "Potassium" = 2,
                                        "Zinc" = 2), bold = T)
}

ktable

The table output by this code looks like this: messed up table

The expected output should be that same table but without all of the extra 1's in the first row below the header.

Upvotes: 0

Views: 445

Answers (1)

user2554330
user2554330

Reputation: 44788

I don't know if this is a kableExtra bug or if you are misusing it, but the problem is in the initial call to kable(). You have

kable(escape = FALSE, align = paste0(rep('l', ncol_table), collapse=""),

and somewhere later, the alignment setting in LaTeX gets repeated many times. If you change that line to

kable(escape = FALSE, align = 'l',

then the problem goes away:

enter image description here

Upvotes: 1

Related Questions