1

Is there a way to conditionally format tableGrob cells/rows when using the gridExtra to export out PDFs? Based on conditional numeric values in a dataframe, I am hoping to change a row color background/fill formatting for my PDF output.

Sample Data:

 Name <- c('John','Adam','Jack')
 a <- c(1, 4, 3)
 b <- c(0, 6, 1)
 df1 <- data.frame(Name, a, b)

Right now I have a simple theme to format the output:

mytheme <- gridExtra::ttheme_default(
    core = list(fg_params=list(cex = 0.6),bg_params=list(fill=c("#FFFFFF", "#F8F8F8"))),
    colhead = list(fg_params=list(cex = 0.6)),
    rowhead = list(fg_params=list(cex = 0.6)))

This is my quick PDF output code:

 pdftitle=paste("Table-1.pdf",sep="")
  pdf(pdftitle,width=14, height=8)
  gridExtra::grid.table(df1,theme = mytheme, rows = NULL)
  dev.off()

The numerical values in column a and b from the sample data are actually successful and unsuccessful events. I'll end up quickly finding the success percentage and add that to my table for the output.

What I am hoping to achieve is the formatting of an entire row of the table in my PDF, so that any value greater than or equal to 50% from df1 will be formatted in this color: #C4122E and if below 50%, formatted as #1B458F

baptiste
  • 75,767
  • 19
  • 198
  • 294
wetcoaster
  • 367
  • 3
  • 15
  • [formattable](https://github.com/renkun-ken/formattable) is pretty nice, though there are other similar options, if you search a bit. – alistaire Oct 06 '16 at 21:36
  • there are several ways to do it, have a look at https://github.com/baptiste/gridextra/wiki/tableGrob – baptiste Oct 06 '16 at 22:33

0 Answers0