I've used R, sweave (but not knitr, and xtable, to create a table, where 1 column is an identifier, and the other three columns are "flag" columns that are either blank or contain a 1 (the flag).
I want to be able to shad the last three columns so each cell is either green (if it is blank) or red (if it contains a 1).
<<xtable3, results=tex>>=
id <- c("1_1", "1_2", "2_1")
a <- c(1,"","")
b <- c("", 1, "")
c <- c("", "", 1)
d <- data.frame(id, a,b,c)
dx <- xtable(d)
align(dx) <- "|c|c|c|c|c|"
print(dx, hline.after=-1:3)
@
Edit: This is the output I get via Sumatra PDF Viewer:
I have made several attempts, unfortunately I didn't save them before posting this question and I cannot recall any of the attempts exactly.
Even if someone could just point me in the right directions I would really appreciate it. I've been able to find information on R and LaTeX, but not information that is for R/Sweave and Latex.
I'm sure there's a clever way to do this with
xtable
, but I'm notoriously bad at gettingxtable
to do what I want. Here's an approach usingpixiedust
that may give you what you need. (The tricky part here is identifying the coordinates of the table that you want to shade).EDIT: I apologize, my original answer didn't satisfy all the requirements you laid out. This has the proper colors with red in the cells with a 1, and green otherwise.
Sweave Adaptation
The same code can be used in Sweave, but requires a couple of adaptations. The equivalent code is:
Here's a Sweave file that will conditionally color xtable cells red or green. The conditional formatting of each cell is accomplished with the LaTeX colortbl and xcolor packages.
Coloring xtable cells with Sweave involves pasting together an escape character ("\"), the LaTeX "\cellcolor" function, an HTML argument, your color choice, and your cell value. You'll want to have columns that look something like this before you convert the data.frame to xtable:
Here is the full Sweave file. I am compiling this in RStudio using knitr and pdfLaTeX, and I'm previewing it in Sumatra.
Edit
Here is the code for compiling with Sweave, not knitr: