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