Skip to content

data_color() vector applies wrong color #633

@jthomasmock

Description

@jthomasmock

Problem:

To specify colors of a column from another column you can in theory specify an equal length character vector of colors. This works nicely for some examples, but fails under some other constraints.

gt version 0.2.1

library(gt)

# create a tibble of letters, numbers
# and a column to "scale" or generate the colors from

dfx <- dplyr::tibble(
  text = letters[1:5],
  numbers = 1:5,
  scale = c(1,2,3,2,1)
)

dfx

#    # A tibble: 5 x 3
#     text  numbers scale
#    <chr>   <int>  <dbl>
#   1 a         1     1
#   2 b         2     2
#   3 c         3     3
#   4 d         4     2
#   5 e         5     1

# create a vector of colors to apply the color from
# should be c(red, blue, green, blue, red)

col_all <- (scales::col_factor(
    palette = c("red", "blue", "green"),
    domain = NULL)(dfx$scale))

scales::show_col(col_all)


# Apply the colors via data_color()
dfx %>% 
  gt() %>% 
  # expected colors
  data_color(
    columns = "text",
    colors = col_all
  ) %>% 
  # expected colors
  data_color(
    columns = "numbers",
    colors = col_all
  ) %>% 
  # applies incorrect color for rows 3, 4, and 5
  data_color(
    columns = "scale",
    colors = col_all
  )

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions