I am using kableExtra to format some tables in an Rmarkdown document. When running the code below, without any position argument to kable_styling, the grouping row labels (the rows in the table where it says "Group 1" and "Group 2") and the footnotes remain left aligned in relation to the table. This is as I would like it.
```{r cars-table, results='asis'}
kable(mtcars[1:10, 1:2], format = "html", caption = "Group Rows",
col.names = c("MPG[note]", "CYL[note]")) %>%
kable_styling("striped", full_width = F) %>%
group_rows("Group 1", 4, 7) %>%
group_rows("Group 2", 8, 10) %>%
add_footnote(c("Some footnote", "Some other footnote"))
```
But when a position argument is provided to kable_styling, the grouping row labels and footnotes seem to take the opposite alignment, rather than remaining left aligned in relation to the table. I say the opposite alignment, as when I use position = "right", the grouping row labels and footnotes become left aligned.
The code below demonstrates the issue when using position = "left".
```{r cars-table, results='asis'}
kable(mtcars[1:10, 1:2], format = "html", caption = "Group Rows",
col.names = c("MPG[note]", "CYL[note]")) %>%
kable_styling("striped", full_width = F, position = "left") %>%
group_rows("Group 1", 4, 7) %>%
group_rows("Group 2", 8, 10) %>%
add_footnote(c("Some footnote", "Some other footnote"))
```
I only load two libraries to make this example and use the defaults when opening an .Rmd document in RStudio.
library(knitr)
library(kableExtra)
What can I do to make the grouping row labels and footnotes left aligned in relation to the table? Thanks.
In kableExtra 0.3.0 or earlier, there was a bug in the position section of kable_styling. The corresponding CSS for left positioning was mistakenly set as text-align:right... Thank you, #meenaparam, for bringing it up!
Now this bug has been addressed in the current dev version and the CRAN version will be updated in a week.
Related
I'm attempting to combine the use of R packages formattable and kableExtra to create a data-table. Using formattable, I'm adding a green thumbs up symbol to one particular column ("b") for numbers > 0, this displays correctly. I then pass my table to "kable" so that I can add the "hover" feature, widen column 1, and add grouped headers. However, whilst the produced data-table correctly displays the "hover" feature and correct grouped headers, the green thumbs up feature (derived from formattable) is missing.
Here is a minimal, reproducible example:
library(formattable)
library(kableExtra)
library(dplyr)
labels <- c("A", "B", "C")
a <- c(0.22, 0.28, 0.23)
b <- c(890.53, 346.84, 1119.63)
c <- c(6.56, 5.70, 4.59)
d <- c(0.0048, -0.3194, -0.2720)
e <- c(-0.3212, 0.1280, 0.0755)
f <- c("-", "-", "-")
df <- tibble(labels,a,b,c,d,e,f)
customGreen = "#71CA97"
# function to assign a thumbs up to numbers > 0
custom_thumb <- formatter("span", style = x ~ style(font.weight = "bold",
color = ifelse(x > 0, customGreen, ifelse(x < 0, customRed, "black"))),
x ~ icontext(ifelse(x > 0, "thumbs-up", ""), x)
)
# use formattable to add thumbs up symbols
df_frmt <- formattable(df, align =c("l","c","c","c","c","c","c"),
list(`labels` = formatter("span"),
`b` = custom_thumb))
# pass the resulting table to kable for further edits
df_kbl <- kbl(df_frmt, escape = T) %>%
kable_styling("hover", full_width = F) %>%
column_spec(1, width = "5cm") %>%
add_header_above(c(" "=2, "Group 1" = 2, "Group 2" = 2, " " = 1))
df_kbl
Given that the hover feature and grouped headers is working well, is the issue something to do with escaping html? I've tried both "escape=T" and "escape=F" in the kable edit though there's no change. I know that both of these packages can be used together from reading the "Integration with formattable" section of this website. I don't know if it's relevant or not, but I'm running this code in an RMarkdown file inside RStudio. Any helps is appreciated!
Following your mentioned link, combining formattable and kableExtra is not done by passing a formattable to the kbl function.
Instead you might use custom (your custom_thumb) or original functions (color_bar or color_tile) from formattable and integrate them into the kableExtra syntax.
df %>%
mutate(b = custom_thumb(b)) %>%
kable("html", escape = F, align = c("l","c","c","c","c","c","c")) %>%
kable_styling("hover", full_width = F) %>%
column_spec(1, width = "5cm") %>%
add_header_above(c(" " = 2, "Group 1" = 2, "Group 2" = 2, " " = 1))
I created a table in an R Markdown document which I want to print in html format using kable and kable_styling from the kableExtra package. One column contains cells that should only show one single asterisk "*".
In the html, these cells are depicted as points / bulletpoints?! How can I change my output in a way that the single asterisks are just printed as asterisks (like the cells containing "**" or "***")?
Example Code:
```{r example}
df <- data.frame(type = c("a", "b", "c", "d"),
number = runif(4),
asterisks = c("*", "*", "**", "***"))
df %>%
kable(caption = "Single asterisk looks like point...") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = F, position = "left")
```
html output:
Any help is highly appreciated. Thanks!
I'm using kableExtra with a github_document and my underscore is not outputting correctly.
Before I tried to escape it, the table was outputting as so:
I then tried to see if I could get rid of the backslashes by escaping the underscore with no luck.
I've tried to escape with \_ \\_ and \\\\_ all of which don't give me the desired result. Using:
\_ R says that it isn't a valid escape command
\\_ prints \_ in my table
\\\\_ prints \\\_ in my table.
My YAML:
---
output: github_document
always_allow_html: true
---
Chunk options:
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
sample code:
names_table <- data.frame(name = c("Heat Index", "Heat Index Extreme Caution", "Heat Index Dangerous", "Temperature over the 95th percentile"), df_name = c("heat\\_index", "heat\\_index\\_ec", "heat\\_index\\_dan", "temp\\_over\\_95\\_pctl"), calculation = c("Heat index calculation", "Temperature between 90-102F", "Temperature between 103-124F", "Temperature exceeds historic 95th pctl"))
names_table %>%
knitr::kable(col.names = c("Variable Name",
"Column Name",
"Variable Calculation"),
escape = TRUE,
format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = F,
position = "center") %>%
column_spec(3, width = "20em")
If I run the code without kable_styling, it runs correctly (i.e. the output is what I want) but if I knit the document, my table still prints with an unwanted backslash.
I've scoured SO with no luck of finding anything to help, but maybe I'm misunderstanding some help somewhere.
Edit: added image of problem, added more information about how the problem arose.
I was able to find a work around for my problem by saving the table as an image and inserting the table into my markdown as an image. Doesn't solve the issue of the output of github_document adding backslashes in front of underscores. Continuing from above:
kable_out <- names_table %>%
knitr::kable(col.names = c("Variable Name",
"Column Name",
"Variable Calculation"),
escape = TRUE,
format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = F,
position = "center") %>%
column_spec(3, width = "20em")
save_kable(kable_out, "man/figures/kable_table.png", zoom = 5)
# zoom preserves image quality
knitr::include_graphics("man/figures/kable_table.png")
I've created a table in RMarkdwon (PDF) with the knitr and kableExtra packages.
I can choose in the kable-function between left,center or right- alignments.
But I want that the content of the table cells are aligned to the decimal points.
In this case it is difficult since there are asterisks behind the numbers indicating the significance.
Here is a reproduceable example:
library(tidyverse)
library(knitr)
library(kableExtra)
tribble(
~Variable_1,~Variable_2,
"13.5","4.4**",
"12.7***","1.2*",
"0.4","0.3***",
"2.3**","11.5**"
)%>%
kable(format = "latex", booktabs=T, escape = T)%>%
kable_styling(position = "center", latex_options = "hold_position")
Which produces this table:
Can someone give me an easy solution directly in R?
If this should not be possible, how would an edit in the latex code look like?
Many thanks in advance!
Here is the table provided by bttomio:
One solution could be intersing the variable names in {} recommended by https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf page 28.
The code then looks like this:
tribble(
~Variable_1,~Variable_2,
"13.5","4.4**",
"12.7***","1.2*",
"0.4","0.3***",
"2.3**","11.5**"
)%>%
rename_all( ~ str_c("{ ",.," }"))%>%
kable(format = "latex", booktabs=T, escape = T)%>%
kable_styling(position = "center", latex_options = "hold_position")
And the table corresponds to the desired output:
But the problem is the full-width-option in the kable_styling-function. If I set this option to TRUE the linebreak of large column names disappears.
Here is a first try:
---
output: pdf_document
header-includes:
- '\usepackage{siunitx}'
- '\newcolumntype{d}{S[table-format=3.2]}'
---
```{r}
library(tidyverse)
library(knitr)
library(kableExtra)
tribble(
~Variable_1,~Variable_2,
"13.5","4.4**",
"12.7***","1.2*",
"0.4","0.3***",
"2.3**","11.5**"
)%>%
kable(format = "latex", booktabs=T, escape = T, align = "d")%>%
kable_styling(position = "center", latex_options = "hold_position")
```
Hi i made this awesome table with kableExtra, but my only problem is that the height of the rows is not always equal. Does any one know a remedy for this?
my table:
for example, as you can see, the line for item number 22 (6th row) has a larger height (spacing) than other lines.
my code:
my_column_names = c("Item number", "Item",
"Emotion", "Social",
"At Home", "Body", "Emotion",
"Social 1", "Social 2",
"At Home", "Body")
kable(df1,
format = "latex", booktabs = TRUE,
col.names = my_column_names,
caption = "Factor loadings for the 4 and 5 Factor Model") %>%
kable_styling(latex_options = c("striped", "hold_position"),
full_width = FALSE) %>%
add_header_above(c(" " = 2,
"4 Factor Model " = 4, "5 Factor model" = 5)) %>%
add_header_above(c(" " = 2,
"Model" = 9)) %>%
kableExtra::landscape()
The reason why the row height is not always equal is that by default kable inserts a \addlinespace every 5th rows. To get rid of it, put linesep = "" in kable(). See Get rid of \addlinespace in kable for details.
Usually, this is something that you can change via CSS in an HTML table. Not sure how to do this with kableExtra but you might want to consider tableHTML to do it. I am adding a small example below to demonstrate row height:
library(tableHTML)
tableHTML(mtcars[1:10, ],
border = 1,
rownames = TRUE,
caption = 'This is a caption',
footer = 'This is a footer',
widths = c(140, rep(50, 11)),
second_headers = list(c(2, 5, 6), c('', 'col2', 'col3')),
theme = 'scientific') %>%
add_css_row(list('height', '50px'), rows = 3:12)
You don't need to use the scientific theme if you don't want to. The package gives you flexibility to add any css you like (like striped rows, etc.). You can check a tutorial here if interested.
P.S. It currently only supports one extra header. Apart from that your whole table can be replicated.