问题
I have this
A<-1:10
B<-10:1
C<-11:20
df<-data.frame(id=1:10,A,B,C,tA=A>5,tB=B>5)
I'm using formattable and this is what I would like to have (Output 1)
formattable(df, list(
id = formatter("span",
style = ~ style(color = "gray")),
A=formatter("span",
style = ~ style(color = ifelse(tA==TRUE, "green", "red")),
~ icontext(ifelse(tA==TRUE, "arrow-up", "arrow-down"), A)),
B=formatter("span",
style = ~ style(color = ifelse(tB==TRUE, "green", "red")),
~ icontext(ifelse(tB==TRUE, "arrow-up", "arrow-down"), B)),
C=color_tile("transparent", "lightpink"),
tA=FALSE,
tB=FALSE))
The problem is I would like to loop for columns A and B.
formattable(df,
lapply(df[,c("A","B")], function(col)
{
col=formatter("span",
style = x ~ style(color = ifelse(x>XXX, "green", "red")),
x ~ icontext(ifelse(x>val[1], "arrow-up", "arrow-down"), x))
}
))
The question: How to combine lapply and formattable to have like in Output 1?
Many thanks.
来源:https://stackoverflow.com/questions/53308518/loop-hide-columns-and-r-formattable