kableExtra how to use cell_spec from a user defined range? - r

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

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:

How to end long table in Rmd pdf with double line

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!

Map cell colors to data values in kableExtra to create a heatmap table

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

rmarkdown & kable/kableextra: Printing % symbol in Table when using escape = F

I want to create a table in a pdf-document using rmarkdwon and kable.
In the header there has to be a linebreak, so to do so i need to set escape = F.
The table itself contains percent-values as strings including the % symbol (for example: "13%").
I found here https://tex.stackexchange.com/questions/34580/escape-character-in-latex that % is a special character in La(Tex) and i have to exclude it using \%.
But how can i create and store a string "\%" in R?
I've tried the following but nothing worked for me:
gsub("%", "\%", c("14%", "15%", "16%"))
Error: '\%' is an unrecognized escape in character string starting ""\%"
gsub("%", "\\%", c("14%", "15%", "16%"))
[1] "14%" "15%" "16%"
> cat("\\%")
\%
gsub("%", cat("\\%"), c("14%", "15%", "16%"))
\%
Error in gsub("%", cat("\\%"), c("14%", "15%", "16%")) :
invalid 'replacement' argument
Does anybody know the solution for this problem?
Thanks in advance
edit:
Here is some example code for my problem:
edit of the edit:
in my made up data.frame testtable i forgot to set stringsAsFactors = F.
Thats what caused the differences to my real-data results.
I've added it to the code and updated the output-pictures
---
title: "Table with % and linbebreak"
output:
# html_document: default
pdf_document:
keep_tex: yes
fig_caption: yes
lang: de
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, error = FALSE, dev=c('pdf','png'), dpi=500)
library(dplyr)
library(data.table)
library(reshape2)
library(knitr)
library(markdown)
library(kableExtra)
library(psych)
library(survey)
library(ggplot2)
set.seed(123)
testtable = data.frame(matrix(paste0(sample(1:100,16, replace = T), "%"), nrow = 4, byrow = T), stringsAsFactors = F)
testtable$group = c("Var1", "Var2", "Var3", "Var4")
testtable = testtable[c(5,1:4)]
colnames(testtable) = c("Variable","All\n(n = 300)", "Group1\n(n = 120)", "Group2\n(n = 100)", "Group3\n(n = 80)")
```
# Table including %-symbol, escape=F not set
```{r}
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
# Table including %-symbol, escape=F
This leads to an Error, see pic below
```{r}
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
# Table without %-symbol, escape=F set
```{r}
# removing the %
testtable[,2:5] = apply(testtable[,2:5],2,function(f) gsub("%", "", f))
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
Somehow %-symbol does not get displayed.
I have no idea how it comes, since i got other table of the same style (just without a linebreak) and the %-symbol works great.
Use way 2 with gsub("%", "\\\\%", f)).
---
title: "Untitled"
author: "Stéphane Laurent"
date: "28 août 2018"
output:
pdf_document:
keep_tex: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, error = FALSE, dev=c('pdf','png'), dpi=500)
library(dplyr)
library(data.table)
library(reshape2)
library(knitr)
library(markdown)
library(kableExtra)
library(psych)
library(survey)
library(ggplot2)
set.seed(123)
testtable = data.frame(matrix(paste0(sample(1:100,16, replace = T), "%"), nrow = 4, byrow = T), stringsAsFactors = F)
testtable$group = c("Var1", "Var2", "Var3", "Var4")
testtable = testtable[c(5,1:4)]
colnames(testtable) = c("Variable","All\n(n = 300)", "Group1\n(n = 120)", "Group2\n(n = 100)", "Group3\n(n = 80)")
```
# Table including %-symbol, escape=F not set
```{r}
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
# Table including %-symbol, escape=F
This leads to an Error, see pic below
```{r}
testtable[,2:5] = apply(testtable[,2:5],2,function(f) gsub("%", "\\\\%", f))
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
Isnt'it what you want? Otherwise I don't understand.
Another solution is to include the percent sign (or any other string of characters including characters you do want to escape) through custom LaTex commands defined before running kable.
```{r}
library(kableExtra)
library(tidyverse)
```
```{r}
test <- tibble(perc = c('4\\Perc', '34.2\\Perc'),
bla = c('bla','blabla'),
otherLatexCommand = c('\\RA', '\\RA'))
```
\newcommand\Perc{\%}
\newcommand\RA{$\Rightarrow$}
```{r}
test %>% kable('latex', escape=F)
```

Set col names and headers (above) to weird characters for tables with knitr, kableExtra and Latex in R markdown

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)

Resources