简体   繁体   中英

In R package Formattable, how to apply digits and conditional formatting at the same time?

I have the object TABLE_LIST which is a list that has tables (I can't provide the contents for privacy policies, sorry).

I first created the object TABLE_LIST (It is a list of data.frames 2x12)

TABLE_LIST=lapply(1:4, function(x) data.frame(rbind(total.ratio4[[x]][-(1)], total.ratio2[[x]][-(1)]), row.names=row))

The following code gives me red and green font colors based on the value on the cell, and it works like a charm:

formattable(TABLE_LIST[[1]], list(area(,-(c(5,10)))~formatter("span", style=x~style(color=ifelse(x>1,"red","green"))),area(,(c(5,10)))~formatter("span", style=x~style(color=ifelse(x>1,"green","red"))))) 

However, I need COLOR AND comma separated numbers. My failed attempt is:

formattable(TABLE_LIST[[1]], list(area(,-(c(5,10)))~formatter("span", style=x~style(color=ifelse(x>1,"red","green"))),area(,(c(5,10)))~formatter("span", style=x~style(color=ifelse(x>1,"green","red"),digits(x,2))),

area(1:2,1:10)~formatter("span",x~ style(digits(x,2))))) 

This code works well, but erases the formatting of the color. I do not know what else to do.

I have to mention I cannot change the original data.frame without messing everything up. So I gotta make the changes on table_list or formattable. Thank you.

I think I solved it. So I will share this small knowledge to people who may have the same problems as me:

formattable(TABLE_LIST[[1]], list(area(,-(c(5,10)))~formatter("span",
style=x~style(color=ifelse(x>1,"red","green")),x~style(digits(x,4))),
area(,(c(5,10)))~formatter("span",style=x~style(color=ifelse(x>1,"green","red")),
x~style(digits(x,4))))) 

Basically, inside the same formatter, on the level of style, add a comma and x~style .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM