1

Is there a way to tell tableGrob via themes to change the color and format (make bold) of a specific line?

I get the following output using tableGrob:

I however would like to reach the following aesthetic and make specific lines bold and change their color like this example:

Based on the following example: enter link description here

 t1 <- ttheme_default(core=list(
        fg_params=list(fontface=c(rep("plain", 4), "bold.italic")),
        bg_params = list(fill=c(rep(c("grey95", "grey90"),
                                    length.out=4), "#6BAED6"),
                         alpha = rep(c(1,0.5), each=5))
        ))

grid.table(iris[1:5, 1:3], theme = t1)

The color of the row is changed every 4 lines. which results to this: result

However, I need to make it specific based on condition.

KRStam
  • 393
  • 5
  • 18
  • Please add reproducible example to your question. And you can easily achieve wanted result with [kableExtra](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) – pogibas Jan 16 '18 at 14:57
  • Are you OK using other packages or want to stick with `tableGrob`? – pogibas Jan 16 '18 at 15:14
  • @PoGibas Thank you for your reply. Yes, unfortunately, I have to stick with tableGrob as far as I am concerned it is the only way to produce grid reports and combine graphs (ggplot2) and tables on the same page as grids. I have achieved this but i don't know how to change colors and format specific lines with tableGrob. My organization won't allow to run R-studio or html, therefore kableExtra may not be achievable solution. – KRStam Jan 16 '18 at 15:19
  • shldn't be difficult to generalize https://stackoverflow.com/questions/18414001/gridextra-colour-different-rows-with-tablegrob to your situation and just change background grob parameters. – hrbrmstr Jan 16 '18 at 16:11
  • Just create a vector of colours thats equal to the number of rows of your data. eg. `dat <- iris[1:10, 1:3] ; cols <- rep("grey95", nrow(dat)) ; rowsToChange <- c(1,5,9) ; cols[rowsToChange] <- "red" ; t1 <- ttheme_default(core=list(bg_params = list(fill=cols))) ; grid.table(iris[1:10, 1:3], theme = t1)` – user20650 Jan 16 '18 at 21:01

1 Answers1

3

If you poke at the link in my comment for how to "find" grobs, then you can do things like:

library(grid)
library(gridExtra)

tg <- tableGrob(iris[1:5, 1:3])

for (i in c(19,24,29)) tg$grobs[[i]] <- editGrob(tg$grobs[[i]], gp=gpar(col="white"))
for (i in c(34,39,44)) tg$grobs[[i]] <- editGrob(tg$grobs[[i]], gp=gpar(fill="blue"))

grid.newpage()
grid.draw(tg)

enter image description here

If this is regular, parameterized report, then it's a "once-and-done" operation to figure out the cell positions and apply your custom formatting.

Otherwise, definitely develop a set of styling functions for targeting "cells" like you would in google sheets or Excel.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Thank you for the answer. Yes, it is indeed a regular parametrized report and I know in advance the number and position of the rows that need to be formated and have bold letters and blue outline. So for instance I need rows 3,6,8,11,12,13 to be marked in blue line. I don't really understand the tableGrob situation and how to target specific rows. Thank you again for your time and any advice is much appreciated. – KRStam Jan 16 '18 at 18:46