Change the size of a title in KableExtra? - r

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

Related

How to force linebreak and bold string in cell within KableExtra

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:

Control table's width when displayed side by side

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:

Avoid repeating the same styling with kable_styling

Using Rmarkdown and kable, I need to repeat several tables with the same formatting but with different datasets.
For example, in the following two chunks, what is changing is the DF, the variable used to sort and the caption.
Is there any way to avoid repeating 90% of the same code for each chunk?
kable(Crib1 %>% arrange(-Z1) %>%
select(Ranking = TYP, SID, Statement, FA1:FA3), longtable = TRUE, booktabs = TRUE, caption = "Crib Sheet - Factor 1") %>%
collapse_rows(1, latex_hline = "major", valign = "top") %>%
kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
column_spec(1, bold=TRUE) %>%
column_spec(3, width = "18em", italic = TRUE) %>%
column_spec(4, bold = TRUE)
kable(Crib2 %>% arrange(-Z2) %>%
select(Ranking = TYP, SID, Statement, FA1:FA3), longtable = TRUE, booktabs = TRUE, caption = "Crib Sheet - Factor 2") %>%
collapse_rows(1, latex_hline = "major", valign = "top") %>%
kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
column_spec(1, bold=TRUE) %>%
column_spec(3, width = "18em", italic = TRUE) %>%
column_spec(4, bold = TRUE)
I am trying to build a function
createKableCrib <- function(factor){
Crib <- rlang::sym(paste("Crib", factor, sep=""))
Z <- rlang::sym(paste("Z", factor, sep=""))
cap <- paste("Crib Sheet - Factor", factor, sep=" ")
kable(!!Crib %>% arrange(!!(-Z)) %>%
select(Ranking = TYP, SID, Statement, FA1:FA3),
longtable = TRUE, booktabs = TRUE, caption = cap) %>%
collapse_rows(1, latex_hline = "major", valign = "top") %>%
kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
column_spec(1, bold=TRUE) %>%
column_spec(3, width = "18em", italic = TRUE) %>%
column_spec(4, bold = TRUE)
}
createKableCrib("1")
But I get the following error:
Error in UseMethod("arrange_") :
no applicable method for 'arrange_' applied to an object of class "name"
Best,
Damien
It looks like you may have gotten there already, but here is an example that seems to work with the use of Curly-Curly instead of the original Bang-Bang (https://www.brodrigues.co/blog/2019-06-20-tidy_eval_saga/).
Didn't have your data, but tried with mtcars and iris. If your columns are the same you could add the select statement after arrange.
library(dplyr)
library(knitr)
library(kableExtra)
create_kable <- function(data, column, title) {
kable({{data}} %>%
arrange({{column}}),
longtable = TRUE, booktabs = TRUE, caption = title) %>%
collapse_rows(1, latex_hline = "major", valign = "top") %>%
kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
column_spec(1, bold=TRUE) %>%
column_spec(3, width = "18em", italic = TRUE) %>%
column_spec(4, bold = TRUE)
}
create_kable(mtcars, mpg, "Crib Sheet - Factor 1")
create_kable(iris, Sepal.Length, "Crib Sheet - Factor 2")
Here is what I found so far.
Not completely satisfactory, as I wanted to be able to create the df name within the function, but I keep on getting errors.
createKableCrib <- function(df, factor){
Z <- paste("Z", factor, sep="")
cap <- paste("Crib Sheet - Factor", factor, sep=" ")
kable(df %>% arrange_at(.vars=desc(Z)) %>%
select(Ranking = TYP, SID, Statement, FA1:FA3),
longtable = TRUE, booktabs = TRUE, caption = cap) %>%
collapse_rows(1, latex_hline = "major", valign = "top") %>%
kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
column_spec(1, bold=TRUE) %>%
column_spec(3, width = "18em", italic = TRUE) %>%
column_spec(4, bold = TRUE)
}
createKableCrib(Crib1, "1")

R Notebook: place knitr::kable tables side-by-side?

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")

kableExtra how to use cell_spec from a user defined range?

First, the package kableExtra indeed produces awesome table. I just have an additional request :
When running this example ( from the manual)
iris[1:10, ] %>% select(-Species) %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "latex", bold = T, color = spec_color(x, end = 0.9),
font_size = spec_font_size(x))
}) %>%
kable("latex", escape = F, booktabs = T, linesep = "", align = "c")
The cells are formated in each column. (1) How to adjust them to the entire table ? (2) Or to a user defined scale ? I noticed that both kableExtra::spec_color and
kableExtra::cell_spec make use of the scales::rescale function which does take a from argument but it's not used in the functions.
Below code of reproducible .Rnw script.
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{booktabs}
\usepackage{float}
\usepackage{colortbl}
\begin{document}
<<tab10, echo=FALSE, results="as.is">>=
require(knitr)
require(kableExtra)
require(tidyverse)
#
<<tab1, echo=FALSE, results="as.is">>=
require(knitr)
require(kableExtra)
require(tidyverse)
iris[1:10, ] %>% select(-Species) %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "latex", bold = T, color = spec_color(x, end = 0.9),
font_size = spec_font_size(x))
}) %>%
kable("latex", escape = F, booktabs = T, linesep = "", align = "c")
#
\end{document}
Okay, with the latest dev ver, you can use scale_from in spec_color, spec_font_size & spec_angle.
library(kableExtra)
library(dplyr)
library(knitr)
iris[1:10, ] %>%
select(-Species) %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "latex", bold = T,
color = spec_color(x, end = 0.9, scale_from = range(unlist(iris[1:10, ]))),
font_size = spec_font_size(x, scale_from = range(unlist(iris[1:10, ]))))
}) %>%
kable("latex", escape = F, booktabs = T, linesep = "", align = "c")

Resources