haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.

Home Page:https://haozhu233.github.io/kableExtra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add_header_above error when data only has 1 row.

KlintQ1 opened this issue · comments

First of all thank you for a great package! I have encountered what might be an edge case but I run into it frequently as I create tables based on subsets of larger data which sometimes results in the subset only having 1 row.

When there is only 1 row in the data add_header_above throws the following error when creating a kable:
"Error in if (bold) { : missing value where TRUE/FALSE needed"

Simplified test examples below to show the issue.

test case not working (nrow = 1)

library(kableExtra)

test <- tibble::as_tibble(data.frame(Test = 1, other = 2))

opts = options(knitr.kable.NA = '')
knitr::kable(test, format = "latex", align = "cc", booktabs = F, col.names = NULL) %>%
kable_styling(full_width = FALSE, latex_options = "hold_position", font_size = 8, position = 'center') %>%
add_header_above(c(paste("Program", "Year", sep="\n"),
paste("Status", sep="\n")),
border_left = T, border_right = T, bold = TRUE, align = "c", color = "#00263E") %>%
column_spec(1, border_left = T) %>%
column_spec(2, border_right = T) %>%
kableExtra::footnote(general = c(paste("basic test")), footnote_order = "general",
general_title = '',
footnote_as_chunk = F,
title_format = c('italic', 'underline'))

Test case working (nrow = 2)

test2 <- tibble::as_tibble(data.frame(Test = c(1,2), other = c(2,3)))

opts = options(knitr.kable.NA = '')
knitr::kable(test2, format = "latex", align = "cc", booktabs = F, col.names = NULL) %>%
kable_styling(full_width = FALSE, latex_options = "hold_position", font_size = 8, position = 'center') %>%
add_header_above(c(paste("Program", "Year", sep="\n"),
paste("Status", sep="\n")),
border_left = T, border_right = T, bold = TRUE, align = "c", color = "#00263E") %>%
column_spec(1, border_left = T) %>%
column_spec(2, border_right = T) %>%
kableExtra::footnote(general = c(paste("basic test")), footnote_order = "general",
general_title = '',
footnote_as_chunk = F,
title_format = c('italic', 'underline'))

Would greatly appreciate if you could look into this.

I remember this issue happened before. I will take a look

This should be fixed on Github

3a6b4db