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)
Related
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.
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 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)
}
Hi I'm running R studio on windows trying to generate a table in Markdown using kableExtra. Recently I notice that colors are working on one of my machine and not another.
library(knitr)
library(kableExtra)
library(Vennerable)
kable(cars, format = "latex", booktabs = T, caption = "Demo Table") %>%
kable_styling(full_width = F) %>%
column_spec(1:1, italic = T, color = "black", background = "yellow") %>%
column_spec(2, bold = T, border_right = T, background = "SpringGreen")
so this runs flawless on one of my pc but in another it give this error.
! Package xcolor Error: Undefined color `SpringGreen'.
I'm not sure of any particular reason why this is so. Anyone have a clue? thanks.
I just added HTML hex color code support to the dev version of kableExtra. If you are willing to use the dev ver, you can do things like.
kable(cars, format = "latex", booktabs = T, caption = "Demo Table") %>%
kable_styling(full_width = F) %>%
column_spec(1:1, italic = T, color = "black", background = "yellow") %>%
column_spec(2, bold = T, border_right = T, background = "#00FF7F")
(I found the hex code for springgreen from http://latexcolor.com/)
I've having some issues with the kableExtra output in RStudio. I'm trying to create a formatted table in an R markdown file once I hit the 'knit' button.
The code is:
kable(temp_table, "html") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "hover", "responsive"), font_size = 11, full_width = FALSE, position = "left") %>%
column_spec(1, bold = T)
It should create a simple table with a header row that I added. But not only does that not show up, but neither do the variable row names. And what precedes the table is a bunch of HTML code that looks like it wasn't rendered.
Some version history:
kableExtra - 0.6.1
knitr - 1.17
rmarkdown - 1.7
R - 3.3.3
RStudio - 1.1.258
Would appreciate any help with this!
---- UPDATE ----
Below is a simple reproducible example that gives me the same kind of output.
---
title: "Iris Example"
output_file: "iris2.html"
---
## Iris Data
``` {r iris, echo = FALSE, warning = FALSE}
library(data.table)
library(knitr)
library(kableExtra)
iris <- data.table(iris)
iris <- iris[, .(
sep_len = mean(Sepal.Length)
, sep_wid = mean(Sepal.Width)
, pet_len = mean(Petal.Length)
, pet_wid = mean(Petal.Width)
), by = .(Species)]
kable(iris, "html") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2)) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "hover", "responsive"), font_size = 10, full_width = FALSE, position = "left") %>%
column_spec(1, bold = T)
```
I've reproduced this output only on an OS X.
Installing the latest version of kableExtra (0.7.0.9000) with
devtools::install_github("haozhu233/kableExtra") solved the problem for me.