I'm writing up some figures and tables in an R Notebook, and I have a few tables I would like to place side-by-side. I am knitting the notebook to a html. The code I have at the moment (below) works, but both tables are aligned to the left. What I would really like is for them to appear side-by-side but also be centered. Any suggestions please? dt_tot and dt_tot_week are data.tables.
knitr::kable(dt_tot, "html", caption = caption) %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
full_width = FALSE, position = "float_left")
knitr::kable(dt_tot_week, "html", caption = caption) %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
full_width = FALSE, position = "float_left")
If you're knitting to HTML, you should be able to use knitr::kables. This gives me two tables, side by side:
library(tidyverse)
library(kableExtra)
knitr::kables(list(
kable(caption = "Left Table",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(),
kable(caption = "Right Table",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling()
)
) %>% kable_styling()
You just need to change the position of table formed by dt_tot_week to float_right instead of float_left. I' am sure that must have been a typo in your code.
knitr::kable(dt_tot, "html", caption ="left Tbl") %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
full_width = FALSE, position = "float_left")
knitr::kable(dt_tot_week, "html", caption ="right Tbl") %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
full_width = FALSE, position = "float_right")
Related
I am writing up an Rmarkdown document and am using this method to output two frames side by side. My caption appears at the bottom and I cannot figure out how to get it to appear at the top.
knitr::kables(
list(
df1[,c(1,3)] %>%
kbl(caption = 'Caption 1',booktabs=T,col.names = c('Col1','col2'),row.names = T) %>%
kable_styling(latex_options = c("striped", "hold_position")),
df2[,c(1,3)] %>%
kbl(caption = 'Caption 2',booktabs=T,col.names = c('Col1','col2')) %>%
kable_styling(latex_options = c("striped", "hold_position"))
)
)
The caption appears at the top when running the chunk in the rmarkdown document but upon knitting - returns captions 1 and 2 at the bottom of each side by side frame.
I have tried many techniques (mostly around editing the raw HTML passed to caption in order to change the size of a title (aka caption) when using KableExtra.
Minimal Reproducible Example
Here's a simple example:
library(knitr)
library(kableExtra)
iris %>%
head %>%
kable(
table.attr = "style = \"color: black;\"",
caption = "<span style='font-size:20'>A lovely title</span>"
) %>%
kable_styling("striped", full_width = T)
But the title size doesn't change:
Span accepts CSS font-size in 3 different ways. Run the following examples to see them in action:
Pixels
library(knitr)
library(kableExtra)
iris %>%
head %>%
kable(
table.attr = "style = \"color: black;\"",
caption = "<span style='font-size:20px'>A lovely title</span>"
) %>%
kable_styling("striped", full_width = T)
Percent
iris %>%
head %>%
kable(
table.attr = "style = \"color: black;\"",
caption = "<span style='font-size:200%'>A lovely title</span>"
) %>%
kable_styling("striped", full_width = T)
"small", "large" etc.
iris %>%
head %>%
kable(
table.attr = "style = \"color: black;\"",
caption = "<span style='font-size:small'>A lovely title</span>"
) %>%
kable_styling("striped", full_width = T)
Read more here
I've got two tables with identical variables but different observations and I've formatted them to sit next to each other side by side to give the appearance of one table using,
knitr::kable(list(df1, df2))
(Apologies I can't show an image as data is confidential, ask me to elaborate further if I'm not making sense)
And I want to make the first column of the first table bold and I tried using
knitr::kable(list(df1, df2)) %>%
kable_paper("striped", full_width = F) %>%
column_spec(1, bold = T)
to do this but that just made the entire first table bold.
Bare with me I'm very new to rmarkdown, any help would be greatly appreciated
Here is one way if you are trying to knit the file in HTML.
---
title: "temp"
output: html_document
---
```{r, echo = FALSE, warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
df1 <- data.frame(col1 = letters[1:5], col2 = rnorm(5))
df2 <- data.frame(col1 = letters[11:15], col2 = rnorm(5))
kable(df1) %>%
kable_paper("striped", full_width = FALSE) %>%
kable_styling(full_width = FALSE, position = "float_left") %>%
column_spec(1, bold = TRUE)
kable(df2) %>%
kable_paper("striped", full_width = FALSE) %>%
kable_styling(full_width = FALSE, position = "left")
```
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:
In Rmd knit to pdf we found out we can end a table with a double line like this:
tibble(v1 = c(1:2),v2 = c(1:2)) %>%
kable(booktabs = TRUE) %>%
kable_styling(position = "left") %>%
row_spec(2, hline_after = TRUE) %>%
row_spec(3, hline_after = TRUE)
When we want to use this for a table that continues on the next page, with use of:
tibble(v1 = c(1:2),v2 = c(1:2)) %>%
kable(booktabs = TRUE, longtable = TRUE) %>%
kable_styling(latex_options = c("repeat_header"),
position = "left") %>%
row_spec(2, hline_after = TRUE) %>%
row_spec(3, hline_after = TRUE)
we see an unwanted * between the two lines. Does anyone know why the * appears, or have a suggestion how to get rid of the *, or a better solution to have a double line at the end of a table? Thanks!