How to adjust table to a pdf page with kable? - r

I am currently working on a Markdown file (with latex) where I use kable() and kableExtra for my tables. The problem is that some of my tables are to big and doesn't fit on a pdf page (even in landscape).
I have tried to use latex_options = "scale_down" from kableExtra but for some reasons it doesn't work, it doesn't change anything. Here is an example of the code I'm running :
kable(dt, "latex", longtable = T, caption = "SampleCaption") %>%
add_header_above(c("","Mens" = 3, "Womens" = 3)) %>%
kable_styling(latex_options = c("striped", "scale_down", "repeat_header"),repeat_header_text = "",
full_width = F) %>%
column_spec(1, width = "10cm")
I already looked on Google and stackoverflow. Anyone have an idea of what I'm doing wrong? Thanks
Edit, here is the working code as requested in comments :
kable(dt, "latex", longtable = T, caption = "SampleCaption") %>%
add_header_above(c("","Mens" = 3, "Womens" = 3, "Total" = 2)) %>%
kable_styling(font_size = 7, latex_options = c("striped", "repeat_header"),repeat_header_text = "",
full_width = F) %>%
column_spec(1, width = "5cm")

This is not an answer but more like a clarification that since scale_down is using the resizebox in package graphicx while longtable is longtable and these two latex packages won't talk with each other, scale_down only works for normal tables.
In fact, you should be seeing a note in your console that "scale_down" doesn't work with longtable
Source in kableExtra
if (table_info$tabular == "longtable") {
warning("Longtable cannot be resized.")
return(x)
}

Related

kable formatting: striped full width table in pdf output of Rmarkdown

I am trying to print a table by using following code in code chunk (Rmarkdown):
{r th_table, echo = FALSE, , warning = FALSE}
kbl(th_df, caption = "analysis", align = 'c',
booktabs = T) %>% kable_styling(latex_options = c("striped", "hold_position"), font_size = 8, full_width = T, position = "center") %>% row_spec(0, bold = T) %>% footnote(symbol = "Total handling time is in seconds")
When I am running this code it prints an output to my pdf document but the table is not striped. As soon as I remove full_width= T option from above code it display the table in striped formatting. Also if I add background="#FFB266" in row_spec(), table is printed without striped
How can I make full width striped table with background color to header or table?
Thanks for any help or suggestion.

why add_footnote not working using kableextra in r-markdown pdf?

I'm trying to add footnote under my table but foot note is not getting displayed.
kbl(dataf,format="latex",longtable = T) %>%
kable_styling(latex_options = c("hold_position","repeat_header"),bootstrap_options = "bordered",full_width = F)%>%
row_spec(0, bold = T)%>%
add_footnote("klvnkkkkkkkkkkkkkkkkkkkkkkkkkkkkk",notation="number")%>%pack_rows("Group 1", 4, 5)

kableExtra addfootnote general spanning multiple lines with PDF (LaTeX) output

Problem
Code
# Toy Data
ID <- c(paste("G0", as.character(1:9), sep = ""),"G10","G11","Mean")
V1 <- c(10.06,11.06,12.06,13.06,14.06,15.06,16.06,17.07,18.07,19.07,6.88,13.86)
V2 <- c(0.21,0.03,0.09,0.03,0.09,0.03,0.09,0.03,0.09,0.21,0.31,NA)
tbl <- data.frame(ID, V1, V1, V2, V1, V2, V1, V2, V2)
colnames(tbl) <- c('ID','Get. \\%','Get. \\%','K','Get. \\%','K','Get. \\%','K','P')
# Specify kable NA value and load kableExtra
options(knitr.kable.NA = '--')
require(kableExtra)
# Generate table for PDF output (LaTeX)
kbl(tbl, format = 'latex', align = 'l', booktabs = T, escape = F, digits = 2,
linesep = "", caption = "This is a table caption.") %>%
add_header_above(c(" ", "AB", "BP" = 2, "CK" = 2, "JAM" = 2, ""), bold = T) %>%
column_spec(1, width = '1.15cm') %>%
row_spec(11, hline_after = T) %>%
row_spec(12, bold = T) %>%
kable_styling(position = "center", latex_options = "hold_position") %>%
footnote(general_title = "Note.", footnote_as_chunk = T,
general = "Relatively long footnote that I would like to span
a couple of lines. Relatively long footnote that I
would like to span a couple of lines.")
Output
Comments
Issue 1: The output displays 'makecell[1]' in the footnote, which I obviously do not want included. Adding the argument escape = T did not resolve this problem as I expected it might have.
N.B. By setting footnote_as_chunk = F, this issue was resolved, but with the unwanted effect of introducing a line break before the caption starts. This is demonstrated by Peter's answer below.
Issue 2 The footnote does not want to be constrained to the length of the table. I suppose one might be able to manually add line breaks in the footnote string, but this seems like tedious work-around, and I'm hoping there is a method for achieving this more efficiently. The documentation shows (see Table 4, p. 25) an example of how one might circumvent this problem, but the code is absent.
EDIT: This issue (#2) was resolved by setting threeparttable = T when calling kbl.
Compiling with pdflatex or xelatex does not seem to make any difference. Any insight would be much appreciated.
Try this:
library(kableExtra)
library(magrittr)
kbl(tbl,
format = 'latex',
longtable = TRUE,
align = 'l',
booktabs = T,
escape = F,
digits = 2,
linesep = "",
caption = "This is a table caption.") %>%
add_header_above(c(" ", "AB", "BP" = 2, "CK" = 2, "JAM" = 2, ""), bold = T) %>%
column_spec(1, width = '1.15cm') %>%
row_spec(11, hline_after = T) %>%
row_spec(12, bold = T) %>%
kable_styling(position = "center", latex_options = "hold_position", full_width = FALSE) %>%
footnote(general_title = "Note.",
footnote_as_chunk = TRUE,
threeparttable = TRUE,
general = "Relatively long footnote that I would like to span a couple of lines. Relatively long footnote that I would like to span a couple of lines.")
With footnote_as_chunk = TRUE using the "general" footnote option "Note." and the "Footnote...." text start on the same line. As in this example, image below.

Extra white space after knitting in Rmarkdown

I am trying to generate a table using kableExtra on the toy iris data. I am able to successfully generate my output in PDF but I get some white spaces after the 5th row.
set.seed(123)
library(dplyr)
library(kableExtra)
df<- slice_sample(iris, n = 10)
kbl(df, caption = "Iris dataset", booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position"))
I couldn't find any trim flags or so that I can pass them to the slice_sample. Not sure why it persists or is it by design?
The line spacing is a result of selecting booktabs = TRUE in the call to kbl which by definition includes '\addlinespace' in the default linesep argument every fifth row to make it easier to scan tables.
You can override this by setting linesep = "". Or for that matter introduce spacing in any order you like.
kbl(df, caption = "Iris dataset", booktabs = TRUE, linesep = "" ) %>%
kable_styling(latex_options = c("striped", "hold_position"))

Reducing column header names font size in kable tables in Rmarkdown (ioslides)

In all my tables that I'm trying to produces in ioslides I am getting a result that shows a massive column name. How do I shrink this to make the table look okay?
Here is one example, but the same thing happens with every table. What am I missing? Ideally I'd want the content in the table to be the same font size.
Markdown:
## Test
``````{r, Test,echo=FALSE}
options("kableExtra.html.bsTable" = T)
kable(df, format= "html") %>%
kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed"), font_size = 12.5)
Image of my output:
You can change row sizes by using row_spec()
Since the header is interpreted as the row 0 you can change your header independently to fit your needings.
Try
kable(df, format= "html") %>%
kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed"), font_size = 12.5) %>%
row_spec(0, font_size=9)
for more settings check row_spec

Resources