r - Color Coding Sweave Only Partially Working -
i still working on this question. i've got gives correct cells red backgrounds, not giving other cells green backgrounds.
note: using sweave only, not knitr.
here code:
<<>>= flag <- data.frame(hosp,dept, overall, dc_info, care_trans) @ <<results=tex>>= color_cells <- function(df, var){ out <- ifelse(df[, var]=="", paste0("\\cellcolor[html]{2db200}{", df[, var], "}"), paste0("\\cellcolor[html]{ff0600}{", df[, var], "}")) } flag$overall <- color_cells(df = flag, var= "overall") flag$dc_info <- color_cells(df = flag, var= "dc_info") flag$care_trans <- color_cells(df = flag, var= "care_trans") @ <<results=tex>>= flagx <- xtable(flag) align(flagx) <- "|c|l|l|c|c|c|" print(flagx[1:40,], hline.after=c(-1:40), sanitize.text.function=identity, sanitize.colnames.function = hmisc::latextranslate) @
a couple of things may contributing:
this subtle , i've missed in earlier questions. in color_cells
function, assigning character string out
, aren't returning value of out
. should write function in 1 of 2 following ways:
color_cells <- function(df, var){ ifelse(df[, var]=="", paste0("\\cellcolor[html]{2db200}{", df[, var], "}"), paste0("\\cellcolor[html]{ff0600}{", df[, var], "}")) }
or
color_cells <- function(df, var){ out <- ifelse(df[, var]=="", paste0("\\cellcolor[html]{2db200}{", df[, var], "}"), paste0("\\cellcolor[html]{ff0600}{", df[, var], "}")) out }
another issue may contributing (though i'm not entirely sure in case) whether columns have na
values. watch happens if value na
: logical comparison doesn't resolve , returns na
without applying color.
> ifelse(na == "", "red", "green") [1] na
you can modify cell_colors function handle the
na`'s like
color_cells <- function(df, var){ df[, var][is.na(df[, var])] <- "" ifelse(df[, var]=="", paste0("\\cellcolor[html]{2db200}{", df[, var], "}"), paste0("\\cellcolor[html]{ff0600}{", df[, var], "}")) }