I try to color the cells (data_a) if it matches to the data_b with kable, I saw the ifelse function in cell_spec but I didn't succeed.
data_a <- data.table("01:05", "01:05", "01:16", "00:33", "00:52")
data_b <- data.table("00:02", "01:05", "02:30")
kable(data_a, row.names = FALSE, format = "html") %>%
kable_styling(full_width = F, position = "center")
Thanks
Try it like this:
library(data.table)
data_a <- data.table(a =c("01:05", "01:05", "01:16", "00:33", "00:52"))
data_b <- data.table(b =c("00:02", "01:05", "02:30"))
library(kableExtra)
data_a %>%
mutate(a = cell_spec(a, "html", color = ifelse(a %in% data_b$b, "green", "red"))) %>%
kable(format = "html", escape = F) %>%
kable_styling("striped", full_width = F)
Related
Below I have a script that contains a 5th text column that had so much written it exceeded the size of the page. Though I added longtable = T and latex_options= "repeat_header" it only continues the table through multiple pages but if the row exceeds the page it gets cut off. How can I keep the table moving along while not losing text.
df %>%
kableExtra::kbl(.,booktabs = T,longtable = T)%>%
row_spec(0,background = "#F6F6F6",color="black")%>%
kable_styling(bootstrap_options = "striped", font_size = 9,latex_options =
c("hold_position","repeat_header"),position = "left") %>%
column_spec(1,width = "2.0cm") %>%
column_spec(2,width = "2.5cm") %>%
column_spec(3,width = "2.5cm") %>%
column_spec(4,width = "4.5cm")%>%
column_spec(5,width="10.0cm")
Here's a workaround by splitting the cell with long text. It works by splitting the text into two chunks based on word count so could easily be adjusted by trial and error.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(wakefield) # for generating long text
library(dplyr)
library(tidyr)
library(stringr)
```
```{r df, include=FALSE}
set.seed(123)
#sample dataset
df <- data.frame(a = 1:6,
b = month.name[1:6],
c = names(mtcars)[1:6],
d = names(islands)[1:6],
e = c(paragraph(2), paste(paragraph(6), collapse = "; "), paragraph(3)))
#create new data frame, cells with long text split into to
df_new <-
df %>%
mutate(f = ifelse(str_length(e)>2000, word(e, 301, -1), NA_character_),
e = ifelse(str_length(e)>2000, word(e, 1, 300), e)) %>%
pivot_longer(cols = c(f, e), values_to = "e") %>%
na.omit() %>%
arrange(a, name) %>%
select(-name)
```
```{r long-table, results='asis'}
df_new %>%
kbl(booktabs = TRUE,
longtable = TRUE) %>%
row_spec(0, background = "#F6F6F6", color = "black") %>%
landscape() %>%
kable_styling(bootstrap_options = "striped",
font_size = 9,
latex_options = c("hold_position","repeat_header"),position = "left") %>%
column_spec(1, width = "2.0cm") %>%
column_spec(2, width = "2.5cm") %>%
column_spec(3, width = "2.5cm") %>%
column_spec(4, width = "4.5cm") %>%
column_spec(5, width = "10.0cm")
```
I am just trying to highlight one cell in my table with kableExtra. The issue that I am having is that some of my cells have $s and ()s. Here is what it looks like
df3 <- data.frame(
"Bitcoin Price:" = c("Snow Panther B1+", "ASICminer 8 nano", "S9", "Avalon 921", "Dragonmint T1", "Edit E11++"),
"3000" = c("($270.71)", "($3376.85)", "($115.80)", "($530.81)", "($1108.14)", "($1035.42)"),
"6000" = c("$1050.37", "($1004.31)", "$666.06", "$547.62", "($245.39)", "$1337.12"),
"9000" = c("$2371.44", "$1368.24", "$1447.92", "$1626.04", "$617.35", "$3709.66"),
stringsAsFactors = FALSE, check.names=FALSE)
I have tried this but it doesn't work
df3 %>%
mutate(
`6000`[,2] = cell_spec(`6000`[,2], color = "red", bold = T)
) %>%
select("Bitcoin Price:", everything()) %>%
kable(align = "c", escape = F) %>%
kable_styling("hover", "striped", full_width = F) %>%
add_header_above(c(" " = 1, "Current Difficulty" = 3)) %>%
add_footnote(c("Statistics Calculated 2019"), notation = "symbol")
Does anyone have any suggestions? I feel like I am close. I am trying to make the cells with the value ($1004.31), red.
Is this what you are looking for?
df3 %>%
mutate(`6000` = cell_spec(`6000`, "html",color = ifelse(`6000` == "($1004.31)", "red", "grey"))) %>%
select("Bitcoin Price:", everything()) %>%
kable(align = "c", escape = F) %>%
kable_styling("hover", "striped", full_width = F) %>%
add_header_above(c(" " = 1, "Current Difficulty" = 3)) %>%
add_footnote(c("Statistics Calculated 2019"), notation = "symbol")
I need to create a latex table in RStudio for pdf output with the following structure:
This table was created for html output with the following code:
mat <- data.frame(a = c("column header","column header"),
rowx=c("row1","row2"),b = c("a","b"),
c = c("x","y"))
kable(mat, align = "c",col.names = c("","","v1","v2")) %>%
kable_styling(bootstrap_options = "striped", full_width = F,
position = "left",font_size = 12) %>%
column_spec(1, bold = T,width="2em",extra_css="transform: rotate(-90deg);") %>%
collapse_rows(columns = 1, valign = "middle") %>%
add_header_above(c(" " = 2, "row header" = 2))
I need to create a similar structure with LaTeX tables.
His is how far I got:
mat <- data.frame(a = c("column header","column header"),
rowx=c("row1","row2"),b = c("a","b"),c = c("x","y"))
kable(mat, align = "c",col.names = c("","","v1","v2")) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12) %>%
collapse_rows(columns = 1, latex_hline = "none") %>%
add_header_above(c(" " = 2, "rows" = 2))
So I still need at least 2 more things:
rotate the label in the very first column
remove the spurious leftmost column separator in the second row.
Can this be achieved with kableExtra commands and parameters?
Here's a shot with huxtable (my package):
as_hux(mat, add_colnames = TRUE) %>%
insert_row(c("", "", "rows", "")) %>%
merge_cells(3:4, 1) %>%
merge_cells(1, 3:4) %>%
merge_cells(1:2, 1:2) %>%
set_rotation(3, 1, 90) %>%
set_bottom_border(0.4) %>%
set_bold(1:2, everywhere, TRUE) %>%
set_wrap(3, 1, TRUE) %>%
set_bottom_padding(4, -1, 48) %>%
set_bottom_padding(3, -1, 30) %>%
set_row_height(c("1em", "1em", "1.5em", "1.5em")) %>%
quick_pdf()
I have to admit, this took a lot of tweaking. TeX tables are hard to understand....
I have R Markdown scripts I run periodically which contain conditional tables with what I'll call violators. Here's an example data frame:
df <- data.frame(Person = c("Jack", "Jill"), Violator = c("F", "F"))
#> Person Violator
#> 1 Jack F
#> 2 Jill F
I only want to show violators (Violator == "T") and there aren't any this month. So my 'normal' kable code below gives me this error, "subscript out of bounds" which I'd expect.
How can I modify my kable code to 'do nothing' if violator does not equal "T". Is ifelse() the way to go? I'm open to kableExtra() solutions.
kable(df %>% filter(Violator == "T"), "html", align = "l") %>%
kable_styling("striped", "hover", full_width = F) %>%
column_spec(1, bold = T, background = "#FFFFFF") %>%
collapse_rows(columns = 1)
This simple approach should work, I think:
```{r}
temp <- df %>% filter(Violator == "T")
if(nrow(temp) != 0){
kable(temp, "html", align = "l") %>%
kable_styling("striped", "hover", full_width = F) %>%
column_spec(1, bold = T, background = "#FFFFFF") %>%
collapse_rows(columns = 1)
}
```
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")