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.
Related
I have this little table:
cases <- c("1182025", "+19525")
deceased <- c("7639", "+25")
TI_7 <- c(1100.395332, 0.0)
df <- data.frame(cases, deceased, TI_7)
If I kable it, the 0.0 in the third column should not appear.
If possible, the data type of the third column should remain numeric, otherwise decimal.mark = ',' does not work.
This is how I kable:
kbl(df, format.args = list(decimal.mark = ',', digits=6), align = "rccc") %>%
kable_paper(bootstrap_options = "striped", full_width = F, position = "left")
I tried it with gsub, but then all the 0s disappear, inside the numbers too.
With other functions the data type changes to unknown.
Ok this seems to work:
df <- na_if(df, 0)
options(knitr.kable.NA = '')
kbl(df, format.args = list(decimal.mark = ',', digits=6), align = "rccc") %>%
kable_paper(bootstrap_options = "striped", full_width = F, position = "left")
UPDATE for why I changed my votes.
This code has the table displayed but R doesn't knit pdf.
all_jt %>%
kbl(longtable = T, booktabs = T,
caption = "table") %>%
remove_column(7) %>%
add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>%
kable_styling(latex_options = c("repeat_header"))
Quitting from lines 13-37 (test_table.Rmd)
Error in remove_column(., 7) :
Removing columns was not implemented for latex kables yet
switching to select(-7) as in here Remove_Column from a kable table which will be output as latex/pdf doesn't work because R doesn't like duplicated column names.
I have two ANOVA tables, jt_1 and jt_2 below, that I want to merge and keep 1 column for the model term only. As I remove the duplicated column, R added .1 to the tail of columns' 7, 8, 9 and 10 names.
library(emmeans)
library(stringr)
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
jt_1 <- print(joint_tests(warp.lm), export = T) %>% as.data.frame()
jt_2 <- jt_1
all_jt <- cbind(jt_1, jt_2) %>%
setNames(gsub("summary.", "", colnames(.)))
all_jt[,-6]%>% #to remove the duplicated column for model term
data.frame(check.names = F) %>%
kbl(longtable = T, booktabs = T,
caption = "table") %>%
add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>%
kable_styling(latex_options = c("repeat_header"))
Here is a brief idea of what I need.
Many thanks in advance.
You can use remove_column function from kableExtra to remove a column instead of all_jt[,-6] which makes the column name unique.
library(knitr)
library(kableExtra)
all_jt %>%
kbl(longtable = T, booktabs = T,
caption = "table") %>%
remove_column(7) %>%
add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>%
kable_styling(latex_options = c("repeat_header"))
R does not like duplicate column names in data.frames. If you step through your last code block line by line you will notice that all_jt[, -6] makes column names unique by adding the ".1" suffix.
The/a solution is to provide column names to kbl directly, e.g.
all_jt[,-6] %>%
kbl(longtable = T, booktabs = T,
col.names = gsub("\\.\\d", "", names(.)),
caption = "table") %>%
add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>%
kable_styling(latex_options = c("repeat_header"))
This produces
I have the following table generated in RMarkdown using the kableExtra package. I'm trying to bold selected words in a cell (the word First in my example below) and force a linebreak between two words in a cell (First and Message), however this doesn't seem to work. Any ideas on how to do this?
library(kableExtra)
library(tidyverse)
first <- c('\\textbf{First} Message','\\textbf{First}\n Message','First Message')
second <- c('Second Message','Second Message','Second Message')
third <- c('Third Message','Third Message','Third Message')
data.frame(first,second,third) %>%
kable(format='latex',caption="Caption",
col.names = c('First',"Second","Third"), booktabs = T, escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position"), font_size = 7) %>%
row_spec(0,bold=T,color = 'white', background = '#7c3042')
You need to add mutate_all(linebreak) %>% to your code.
Check the documentation here (page 26): https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
Modifying your code:
library(kableExtra)
library(tidyverse)
first <- c('\\textbf{First} Message','\\textbf{First}\n Message','First Message')
second <- c('Second Message','Second Message','Second Message')
third <- c('Third Message','Third Message','Third Message')
data.frame(first,second,third) %>%
mutate_all(linebreak) %>%
kable(format='latex',caption="Caption",
col.names = c('First',"Second","Third"), booktabs = T, escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position"), font_size = 7) %>%
row_spec(0,bold=T,color = 'white', background = '#7c3042')
Result:
I have a table that I am creating for a pdf presentation using kable and kableExtra. I am wanting to group the rows and I need to use superscripts in the row group labels. I have tried several different things. Here is an example of some of the methods I have tried so far.
library(kable)
library(kableExtra)
foo <- data.frame(a = 1:10, b = 11:20, c = 21:30)
kable(foo, format = "latex", booktabs = T, row.names = FALSE, linesep = "", escape = FALSE) %>%
kable_styling(latex_options = c("striped")) %>%
group_rows("Group1<sup>a</sup>", 1, 2) %>%
group_rows(paste0("Group2", footnote_marker_alphabet(1), sep = ""), 3, 4) %>%
group_rows(expression("Group3"^a), 5, 6) %>%
group_rows("Group4\\textsuperscript{a}", 7, 8)
I have run out of ideas and haven't been able to find any additional suggest in my search.
You need escape=FALSE in your group_rows() calls to allow the latex commands to be interpreted. You also seem to need to double each backslash (I don't quite understand why). After that, there are a few different options that work:
kable(foo, format = "latex", booktabs = T, row.names = FALSE, linesep = "", escape = FALSE) %>%
kable_styling(latex_options = c("striped")) %>%
group_rows("$\\\\text{Group1}^a$", 1, 2, escape = FALSE) %>%
group_rows(paste0("Group2\\\\", footnote_marker_alphabet(1), sep = ""), 3, 4, escape = FALSE) %>%
# I don't think expression() is helpful, doesn't seem to get converted
# to latex
group_rows(expression("Group3"^a), 5, 6) %>%
group_rows("Group4\\\\textsuperscript{a}", 7, 8, escape = FALSE)
I'm trying to create a table in Rmarkdown (for pdf) based on the diamonds data set and I want to change some column names and headers above the column names, I would like to know how I can set some headers in italic or bold and some headers should be a symbol (like the one for partial eta squared) or a complete formula. I've included my R code (and I have installed Latex) with the table as a picture (not adjusted yet).
```{r setup, include=FALSE}
setwd("~/Desktop/Tables")
knitr::opts_chunk$set(echo = FALSE)
# global options
options(knitr.table.format = "latex")
# show space instead of NA in tables
options(knitr.kable.NA = '')
library(tidyverse)
library(knitr)
library(kableExtra)
df = diamonds
```
```{r message=FALSE, warning=FALSE}
df_table = df %>%
summarise(avg = round(mean(price), 2),
sd = round(sd(price), 2),
n = n(),
range = round(max(price), 2)) %>%
mutate(grouping = "Total") %>%
select(grouping, avg, sd, n, range) %>%
bind_rows(df %>%
group_by(cut) %>%
summarise(avg = round(mean(price), 2),
sd = round(sd(price), 2),
n = n(),
range = round(max(price), 2)) %>%
mutate(grouping = as.character(cut)) %>%
select(grouping, avg, sd, n, range))
kable(df_table,
booktabs = TRUE,
linesep = "",
col.names = c("Grouping", "M in italic", "SD in italic", "N not
italic", "some weird formula for the range" )) %>%
kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
add_header_above(c(" " = 1, "the formula for the variance" = 4))
```
Set escape = FALSE in your call to kable and add_header_above and use LaTeX in your column headings. I should admit that it's a mystery to me exactly how many backslashes should be added to get the desired result.
kable(df_table,
booktabs = TRUE,
linesep = "",
col.names = c("Grouping", "$M$", "$SD$", "N", "$\\eta$"), escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
add_header_above(c(" " = 1, "$\\\\operatorname{Var}[X]$" = 4), escape = FALSE)