renkun-ken / formattable

Formattable Data Structures
Other
694 stars 78 forks source link

invisible white text when rendering a table in knitr chunk #144

Open verajosemanuel opened 3 years ago

verajosemanuel commented 3 years ago
products <- data.frame(id = 1:5, 
                        price = c(10, 15, 12, 8, 9),
                        rating = c(5, 4, 4, 3, 4),
                        market_share = percent(c(0.1, 0.12, 0.05, 0.03, 0.14)),
                        revenue = accounting(c(55000, 36400, 12000, -25000, 98100)),
                        profit = accounting(c(25300, 11500, -8200, -46000, 65000)))
formattable(products, list(
  price = color_tile("transparent", "lightpink"),
  rating = color_bar("lightgreen"),
  market_share = color_bar("lightblue"),
  revenue = sign_formatter,
  profit = sign_formatter))

Captura

monkeywithacupcake commented 3 years ago

What do you want to happen instead? You want black text?

verajosemanuel commented 3 years ago

Visible text should suffice.

monkeywithacupcake commented 3 years ago

I think you maybe have another setting, like a default to white text?

When I use your code , I get black text knitted.

Screen Shot 2021-10-16 at 11 34 37

However, when I have an issue with color, possibly that needs to be changing with the background, I write a custom function.

Here's an example, stoplight tile...which turns text white with a red background but otherwise has black text.

stoplighttile <- function(cut1 = .1, cut2 = .2, fun = "comma", digits = 0) {
  fun <- match.fun(fun)
  formatter("span", x ~ fun(x, digits = digits),
            style = function(y) style(
              display = "block",
              padding = "0 4px",
              "border-radius" = "4px",
              "color" = ifelse( y >= cut2, csscolor("#FFFDF9"), csscolor("black")),
              "background-color" = ifelse( y < cut1, csscolor("#50D890"),
                                           ifelse( y < cut2, csscolor("#F6DA63"),
                                                   csscolor("#E32249")))
            )
  )
}

with your data,

formattable(products, list(
  price = color_tile("transparent", "lightpink"),
  rating = color_bar("lightgreen"),
  market_share = stoplighttile(cut1 = 0.05, cut2 = 0.1, fun = "percent", digits = 0)
  )
)

The result is:

Screen Shot 2021-10-16 at 11 41 33

NOTE: I used the tile function on the market share column and not on the price column where you were having the issue, but I hope it makes sense.