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)
Related
I've finally made my dream table with kableExtra, but it's doing very weird things with kable_styling(latex_options=c("repeat_header")), where it's writing the headings over my column labels. Here I'm using the example dataset diamonds. It is also moving the first column below where it should be (red arrows in picture).
(Also can't download LaTex because of work authorization issues, so that's not an option)
Thanks for all the help!
library(knitr)
library(kableExtra)
library(dplyr)
options(
knitr.table.toprule = '\\toprule',
knitr.table.midrule = '\\midrule',
knitr.table.bottomrule = '\\bottomrule'
)
df1 <- read.csv("diamonds.csv")
a11<-df1%>%
group_by(cut, color, clarity)%>%
summarize_at( .vars=c("price"),
.funs=~mean(.,na.rm=TRUE)) %>%
mutate(sort = 3)
b11<-df1%>%
group_by(cut, color) %>%
summarize_at( .vars=c("price"),
.funs=~mean(.,na.rm=TRUE)) %>%
mutate(clarity="", sort = 2) %>%
select(cut, color, clarity,everything())
c11<-df1 %>%
group_by(cut) %>%
summarize_at( .vars=c("price"),
.funs=~mean(.,na.rm=TRUE)) %>%
mutate(color="", clarity= "", sort = 1) %>%
select(cut, color,everything())
table3<-rbind(a11,b11,c11)
table3%>%
arrange(cut, color, clarity) %>%
select(-sort)%>%
filter(price>=3000)%>%
kbl(
caption = "Table",
longtable=T, booktabs=T) %>%
add_header_above(header = c("Table 1." = 4))%>%
kable_styling(latex_options=c("repeat_header"))%>%
kable_paper(full_width = F) %>%
column_spec(1, bold = T) %>%
column_spec(3, italic = T) %>%
collapse_rows(columns = 1:3, valign = "top")
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)
I am listing my results in RMarkdown. I want to show max values in each column in bold. How can I do that?
See the code, and output (as png) below please.
data<-data.frame(A=c(1,2,4,3), B=c(8,7,9,10), C=c(14,12,13,11), D=c(15,18,17,16))
rownames(data)<-c("E", "F", "G", "H")
library(knitr)
library(kableExtra)
kable(data) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c( '', Group1 = 2, Group2 = 2))
We could do this with cell_spec
Loop across the columns of the dataset, add the cell_spec layer with bold argument as a logical vector i.e. TRUE where the column value is max with ==
Convert to kable and use the OP's code as in the post
library(dplyr)
library(knitr)
library(kableExtra)
data %>%
mutate(across(everything(), ~ cell_spec(., bold = . == max(.)))) %>%
kable(escape = FALSE, booktabs = TRUE) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c( '', Group1 = 2, Group2 = 2))
-output
If this needs to be by row maxs, then an option would be to transpose
data %>% t %>%
as.data.frame %>%
mutate(across(everything(), ~ cell_spec(., bold = . == max(.)))) %>%
t %>%
as.data.frame %>%
kable(escape = FALSE, booktabs = TRUE) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c( '', Group1 = 2, Group2 = 2))
-output
Or for the rowwise, create the cell_spec layer with apply
data[] <- t(apply(data, 1, function(x) cell_spec(x, bold = x == max(x))))
data %>%
kable(escape = FALSE, booktabs = TRUE) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c( '', Group1 = 2, Group2 = 2))
Or may use dapply from collapse for faster execution
library(collapse)
dapply(data, MARGIN = 1, FUN = function(x) cell_spec(x, bold = x == fmax(x))) %>%
kable(escape = FALSE, booktabs = TRUE) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c( '', Group1 = 2, Group2 = 2))
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.
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")