Percentage is not working on Formattable

I'm customizing a table with the formattable package following the examples provided in this blog.
Basically, what I'm trying to do is set the values as percentage and then color them using the color_tile function.
Here's a reprex of my code:

library(formattable)

# my data
data <- data.frame(
 Class = c("A","B", "C"),
 P1 = c(30, 40, 30),
 P2 = c(40, 40, 20),
 P3 = c(50, 25, 25)
 )

# The table with percentages
formattable(data, align = c("c","c","c","c"), 
            list(`Class` = formatter("span",
                                     style = ~ style(color = "black",
                                                     font.weight = "bold")),
                 area(col = 2:4) ~ function(x) percent(x/100, digits = 0)))

# And then color
formattable(data, align = c("c","c","c","c"), 
            list(`Class` = formatter("span",
                                     style = ~ style(color = "black",
                                     font.weight = "bold")), 
                 area(col = 2:4) ~ function(x) percent(x / 100, digits = 0),
                 area(col = 2:4) ~ color_tile("#DeF7E9", "#71CA97")))

As you can see, I can set the values as percentages in the first table:
tabla1
But when I try to color them, the percentages disappear as you can see in the second table:
Tabla2
So, my question is: what's the issue with my code?

Thanks in advance! :v:

1 Like

Hi, it seems that the color_tile line supersedes the previous percent line. Is there a way of combining the two instructions (% and color) into a single function statement? Everything I've tried has failed.

If you switch them around, you get just the percent formatting and not the colouring:

library(formattable)
#> 
#> Attaching package: 'formattable'
#> The following object is masked from 'package:crayon':
#> 
#>     style

# my data
data <- data.frame(
  Class = c("A","B", "C"),
  P1 = c(30, 40, 30),
  P2 = c(40, 40, 20),
  P3 = c(50, 25, 25)
)

# And then color
formattable(data, align = c("c","c","c","c"), 
            list(`Class` = formatter("span",
                                     style = ~ style(color = "black",
                                                     font.weight = "bold")), 
                 area(col = 2:4) ~ color_tile("#DeF7E9", "#71CA97"),
                 area(col = 2:4) ~ function(x) percent(x / 100, digits = 0)))

Class

P1

P2

P3

A

30%

40%

50%

B

40%

40%

25%

C

30%

20%

25%

Created on 2020-09-28 by the reprex package (v0.3.0)

1 Like

Looks like this might be connected to this issue.
Have you tried installing the latest formattable from github?

Looks like the above issue was fixed some time ago in version 0.2.0.2 but the package author hasn't been able to get this version available on CRAN yet.

remotes::install_github("renkun-ken/formattable")
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Hi,
Yes, this solve the problem!
I was trying combining the function without success, but now it´s working perfectly.

thanks a lot!

1 Like