formattable: change font of a column and apply color_bar on the same column

Viewed 1123

I need some help in adding two formatting features to a column using formattable (actually, I use format_table, but I think it's the same usage). I can add color bars:

data <- data.frame(id = 1:10, x = round(rnorm(10, 100, 25),0))
myhtml<-format_table(data,list(x=color_bar("lightgreen")))
write(myhtml,outfile)

enter image description here

And I can format fonts

myhtml<-format_table(data,list(x=formatter("span",style="font-family:verdana; font-size:8")))
write(myhtml,outfile)

enter image description here

But I really am not able to have the two work together :-(

Basically I do not understand how I can pass both color_bar and formatter to the same column.

Any help would be appreciated.

EDIT: MY SOLUTION

I found this demo code (which is a slight modification fo the code for color_bar)

And it didn't take too much to answer my own question adding a couple of lines:

myhtml<-format_table(data, list(x = formatter("span",
  style = x ~ style(
    display = "inline-block",
    direction = "rtl",
    "border-radius" = "4px",
    "padding-right" = "2px",
    "background-color" = csscolor("lightgray"),
    width = percent(proportion(x)),
    "font-family" = "verdana",
    "font-size" = 10,
    color = csscolor(gradient(x, "darkgreen", "red"))
  ))))
write(myhtml,outfile)

enter image description here

0 Answers
Related