r - Conditional formatting FlexTable -
i looking way conditionally format flextable reporters using percentage figures. below small example.
packs <- list("reporters","scales") lapply(packs,require,character.only = true) dat <- cbind.data.frame(rep("abcd",2),c(0.07,0.11),c(0.03,0.01)) colnames(dat) <- c("a","b","c") dat[,2] <- percent(dat[,2]) pp = pptx() pp = addslide(pp, "title , content") datft = flextable(data = dat) cname <- c("b","c") (i in cname) { if (i=="b") { datft[dat[, i] <= 10, i] = cellproperties( background.color = "green" ) datft[dat[, i] > 10, i] = cellproperties( background.color = "red" ) } else if (i=="c") { datft[dat[, i] <= 0.02, i] = cellproperties( background.color = "green" ) datft[dat[, i] > 0.02, i] = cellproperties( background.color = "red" ) } } pp = addflextable(pp, datft) writedoc(pp, paste(getwd(),"/example.pptx",sep=""))
this works fine column c not column b, it's not numeric. couldn't figure out way apply function formats values percentage figures after backgroundcolor changed.
try (with explanation in comment after testing):
for (i in cname) { if (i=="b") { datft[as.numeric( sub("%", "", dat[, i])) <= 10, i] = cellproperties( background.color = "green" ) datft[ as.numeric( sub("%", "", dat[, i])) > 10, i] = cellproperties( background.color = "red" ) } else if (i=="c") { datft[dat[, i] <= 0.02, i] = cellproperties( background.color = "green" ) datft[dat[, i] > 0.02, i] = cellproperties( background.color = "red" ) } }
Comments
Post a Comment