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 thena`'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], "}")) }  

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo