I need to mark some special cells in the table with a gray color. Something like this:
```{r}
library(huxtable)
library(magrittr)
sample_df <- data.frame(matrix(1:25, nrow = 5))
sample_df %>% as_huxtable() %>% set_all_borders(1) %>%
set_background_color(row = 2, col = 1, value = "grey") %>%
set_background_color(row = 3, col = 2, value = "grey") %>%
set_background_color(row = 4, col = 3, value = "grey") %>%
set_background_color(row = 5, col = 4, value = "grey") %>%
set_background_color(row = 6, col = 5, value = "grey")
```
And after "knitr" as HTML document it gives me the following (as a screenshot):
And that's what i need to get. BUT, my question is: What is more elegant way to do it instead of writing such strings of code? I tried to do it like this:
my_fan <- function(.data) {for (i in c(2:6))
{set_background_color(.data, row = i, col = i-1, value = "grey")}
.data
}
sample_df %>%
as_huxtable() %>% set_all_borders %>%
my_fan()
... And it doesn't give me any result at all. Any ideas?
You can use the old-school interface and a little-known fact about R subsetting:
From
?Extract
:Or if you want to be really cool:
I'm amazed this worked :-)