Kables isn't seeing my kable_styling attributes? - r

I'm trying to spit out two tables side by side, using knitr::kables.
I can't figure out how to make my styles stick, though. If I run one kable, the styles work fine:
kable(
caption = "Oh look! A Caption",
starwars %>%
count(gender, sex) %>%
arrange(desc(gender))
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
gives me a tidy looking table:
But if I try to set two up side by side, the formatting (or kable_styling) gets lost:
knitr::kables(list(
kable(caption = "Oh look! A Caption",
starwars %>%
count(gender, sex) %>%
arrange(desc(gender))) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed")),
kable(caption = "Oh look! A Caption",
starwars %>%
count(gender, sex) %>%
arrange(desc(gender))) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
))
The formatting all just evaporates:
How do I get kable_styling to apply to two kables?

The extremely unsatisfying solution turned out to be adding a third kable_styling() call:
knitr::kables(list(
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed")),
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
)
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
That seems verbose, but it works.

Related

Change the size of a title in KableExtra?

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

There are multiple tibbles in one codechunk. How can show them after knit in r markdown? Everytime I only get the last tibble shown

In R markdown file, I want to the result show all the tibbles:
df_with_desc_reorder_total,df_with_desc_reorder_first,df_with_desc_reorder_second,df_with_desc_reorder_third
But everytime I knit, I only got the last tibbledf_with_desc_reorder_third.
And I can't break this to multiple chunks, cause this is in a IF statement.
many thanks.
df_total <- df %>% filter(create_de_time >= third_shift_start & create_de_time <= second_shift_end) %>%
group_by(ITEM_CODE) %>%
summarise(totoal_booking = sum(QUANTITY))
#df_total
df_with_desc_total <- df_total %>% inner_join(grouped_description_in_20, by=c("ITEM_CODE"="ITEM"))
df_with_desc_reorder_total <- select(df_with_desc_total,c("ITEM_CODE","DES","totoal_booking"))
df_with_desc_reorder_total %>% kable("html") %>% kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)
df_first <- df %>% filter(create_de_time >= first_shift_start & create_de_time <= first_shift_end) %>%
group_by(ITEM_CODE) %>%
summarise(totoal_booking = sum(QUANTITY))
#df_first
df_with_desc_first <- df_first %>% inner_join(grouped_description_in_20, by=c("ITEM_CODE"="ITEM"))
df_with_desc_reorder_first <- select(df_with_desc_first,c("ITEM_CODE","DES","totoal_booking"))
df_with_desc_reorder_first %>% kable("html") %>% kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)
df_second <- df %>% filter(create_de_time >= second_shift_start & create_de_time <= second_shift_end) %>%
group_by(ITEM_CODE) %>%
summarise(totoal_booking = sum(QUANTITY))
df_with_desc_second <- df_second %>% inner_join(grouped_description_in_20, by=c("ITEM_CODE"="ITEM"))
df_with_desc_reorder_second <- select(df_with_desc_second,c("ITEM_CODE","DES","totoal_booking"))
df_with_desc_reorder_second %>% kable("html") %>% kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)
df_third <- df %>% filter(create_de_time >= third_shift_start & create_de_time <= third_shift_end) %>%
group_by(ITEM_CODE) %>%
summarise(totoal_booking = sum(QUANTITY))
df_with_desc_third <- df_third %>% inner_join(grouped_description_in_20, by=c("ITEM_CODE"="ITEM"))
df_with_desc_reorder_third <- select(df_with_desc_third,c("ITEM_CODE","DES","totoal_booking"))
df_with_desc_reorder_third %>% kable("html") %>% kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)

Side by side kables return "Not in outer par mode"

This is presumably related to latex kable side-by-side tables "Not in outer par mode" but the solution identified by the author, to use latex_options = c("Hold_position") is not changing the outcome for me when I try to knit to PDF. The author's full comment reads:
I got the tables to display side-by-side by adding kable_styling(latex_options = c("striped", "Hold_position")).
Here is a minimal example that works fine:
---
title: "Example Document"
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y at %H:%M ')`"
output: pdf_document
---
```{r setup, include=FALSE, echo=FALSE}
library(tidyverse)
library(kableExtra)
```
``` {r species}
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))
```
``` {r planet}
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
)%>% kable_styling(latex_options = c("striped", "Hold_position"))
```
When I use knitr::kables to combine them into a pair of side-by-side tables, I can run the code chunk and knit to HTML just fine, but I'm still getting a "not in outer par mode" error:
! LaTeX Error: Not in outer par mode.
Error: LaTeX failed to compile 10_knit_doesnt.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See 10_knit_doesnt.log for more info.
Execution halted
The actual code chunk is this:
``` {r side by side}
knitr::kables(list(
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(),
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling()
)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))
```
I've tried moving the styling (kable_styling(latex_options = c("striped", "Hold_position")) ) into the individual tables, but that doesn't seem to have any impact.
knitr::kables(list(
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position")),
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))
)
) %>% kable_styling()
Can I knit to PDF while using side-by-side tables?

How do I pass a variable to "count()" inside a function?

There's a block of code that I'm using over and over in a project. It works fine when I run it straight:
kable(
starwars %>%
count(gender),
col.names = c("Response", "Count")
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
But I want to create a function that I can just call once to run it. I expected that this would work:
fancy_table <- function(x,y) {
kable(
y %>%
count(x),
col.names = c("Response", "Count")
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
}
But when I try to run it with fancy_table(gender, starwars), I get an error:
Error: Column `x` is unknown
How do I pass a function variable to count()?

how to specify dynamically last row number in row_spec kable()?

I am generating different tables of different row length, so I want to have all the text of certain color, but I have a question about the last row of my code in row_spec
library(kable)
library(kableExtra)
mtcars %>% filter(cyl=4) %>%
kable(align=c("l", rep("c", ncol(.)-1)),bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"), full_width = TRUE) %>%
row_spec(0: nrow(.), color = "black")
0: nrow(.) is not valid and I am not sure why, while rep("c", ncol(.)-1)) works.
I think it doesn't work because nrow(.) returns NULL:
library(kable)
library(kableExtra)
mtcars %>%
kable(align=c("l", rep("c", ncol(.)-1)),bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"), full_width = TRUE) %>% nrow(.)
#NULL
You could do this to color all rows:
mtcars %>%
kable(align=c("l", rep("c", ncol(.)-1)),bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"), full_width = TRUE) %>%
row_spec(1:nrow(mtcars),color = "black")
Even though not elegant, I would do it in two steps:
library(knitr)
library(kableExtra)
library(dplyr)
# Step 1: Prepare data
temp <- mtcars %>%
filter(cyl == 4) %>%
sample_n(sample(2:nrow(.), 1)) %>%
select(1:3)
# Step 2: Produce table
temp %>%
kable(align=c("l", rep("c", ncol(temp)-1))) %>%
kable_styling(c("striped", "hover", "condensed"), full_width = FALSE) %>%
# Format last row:
row_spec(nrow(temp), color = "red", italic = TRUE, bold = TRUE)

Resources