Is there a way to control each table's width separately when displayed side-by-side like in the example below?
```{r sample, echo=FALSE}
library(knitr)
library(kableExtra)
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
```
```{r, echo = FALSE}
kable(t1) %>%
kable_styling(full_width = FALSE, position = "float_left")
kable(t2) %>%
kable_styling(full_width = FALSE, position = "left")
```
For example, I would like the first table below to have 2 times the width of the second table.
I tried kable(format = 'html', table.attr = "style='width:80%;'") but it doens't work and I don't know why. The width doesn't change at all.
I tried kable(format = 'html', table.attr = "style='width:80%;'") but
it doens't work and I don't know why. The width doesn't change at all.
You were very close, just specify full_width to TRUE:
```{r, echo = FALSE}
kable(t1, format = "html", table.attr = "style = 'width: 69%;'") %>%
kable_styling(full_width = TRUE, position = "float_left")
kable(t2) %>%
kable_styling(full_width = FALSE, position = "left")
```
Output:
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.
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 a table below and would like to apply ROW-level heat maps.
(1) Any idea how? Right now the heat map is the same for both values in the single row below.
(2) Is there a way to make the header for the group column NOT be angled 90 degrees? Right now all headers are angled but for the group column it be better without angle=90.
here is the rmd file.
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(kableExtra)
#d = data.frame(group= c("A","b"),cat1=c(2,50),cat2=c(100,2))
d = data.frame(group= c("A"),cat1=c(2),cat2=c(NA))
d = d %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "latex", bold = F, background = spec_color(x,option="C", begin=.5, end = 0.99))
})
```
```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8}
kable(
d, format ="latex",
caption = "",
booktabs = T,
longtable = T,
escape = F ,
align = "c"
) %>% kable_styling(latex_options = c(
"striped",
"repeat_header"
)
)%>% row_spec( 0,angle = 90)
```
Note: Ideally it'd be good to have this done with the kableExtra functionality so they color schemes match exactley to other kableExtra tables.
I'm actually not sure how to get the desired color mapping from spec_color. As an alternative, in the code below I've generated the color mapping using the colorRamp function and a palette-generating function, which can be adjusted as desired. The code also deals with missing values by coloring them light gray.
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(viridis)
library(dplyr)
library(kableExtra)
set.seed(2)
d = data_frame(group = sample(LETTERS[1:5], 10, replace=TRUE), cat1=runif(10, 0, 100), cat2=runif(10, 0, 100))
d[5,2] = NA
max.val = max(d[ , sapply(d, is.numeric)], na.rm=TRUE)
#pal.fnc = colorRamp(viridis_pal(option="C")(2))
pal.fnc = colorRamp(c("red", "yellow", "green"))
d = d %>%
mutate_if(is.numeric, function(x) {
cell_spec(round(x,1), "latex", bold = F, color=grey(.3),
background = rgb(pal.fnc(x/max.val) %>% replace(., is.na(.), 200), maxColorValue=255))
})
```
```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8}
kable(
d, format ="latex",
linesep="",
caption = "",
booktabs = T,
longtable = T,
escape = F ,
align = "c"
) %>% kable_styling(latex_options = c(
"striped",
"repeat_header"
)
)
```
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")