award/medal emojis loses colors

Problem Description

I tried out the {emo(ji)} package with award/medal emojis glued to a table created by {gt} but the saved table resulted in a color loss in both emojis. Kindly, help.

Code Snippets

emo::ji_glue(award("Gold"))
emo::ji_glue(award("Silver"))
emo::ji_glue(award("Bronze"))

gtsave(out, "standings.png")

Example

image

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like
# Sample data
sample_data <- data.frame(TEAM = c("KENYA", "NEW ZEALAND", "FIJI"),
                          POINTS_LEG1 = c(22, 19, 17),
                          POINTS_LEG2 = c(17, 22, 19),
                          POINTS_LEG3 = c(17, 19, 22))

# View sample data
head(sample_data)
#>          TEAM POINTS_LEG1 POINTS_LEG2 POINTS_LEG3
#> 1       KENYA          22          17          17
#> 2 NEW ZEALAND          19          22          19
#> 3        FIJI          17          19          22
datapasta::df_paste(head(sample_data))
#> data.frame(
#>   stringsAsFactors = FALSE,
#>               TEAM = c("KENYA", "NEW ZEALAND", "FIJI"),
#>        POINTS_LEG1 = c(22, 19, 17),
#>        POINTS_LEG2 = c(17, 22, 19),
#>        POINTS_LEG3 = c(17, 19, 22)
#> )

# Upload the packages
pacman::p_load(tidyverse, gt, emo, emoji)

# Runnable code
out <- sample_data %>% 
  gt() %>% 
  text_transform(
    locations = cells_body(columns = POINTS_LEG1:POINTS_LEG3),
    fn = function(x) {
      paste0(x, "       ",
             dplyr::case_when(
               x == 22 ~ emo::ji_glue(medal("Gold")),
               x == 19 ~ emo::ji_glue(medal("Silver")),
               x == 17 ~ emo::ji_glue(medal("Bronze"))))
      }
    )


gtsave(out, "sample_Emojis.png")

Created on 2022-06-15 by the reprex package (v2.0.1)

Expected a result similar to this or a better display of medals

image

You've made a good quality reprex.
I was going to suggest you to raise an issue with gt, I suspect a webshot issue, then i saw that someone raised this previously so its a known issue.
Emojis in GT Tables loses colors · Issue #889 · rstudio/gt (github.com)

1 Like

Going one step further, in advance of gt officially changing their functions to use weshot2 rather than webshot, we can go ahead and make our own save function based on theirs with the webshots swapped out.



library(webshot2)
new_gt_save <- function (data, filename, path = NULL, ..., zoom = 2, expand = 5) 
{
  filename <- gt:::gtsave_filename(path = path, filename = filename)
  tempfile_ <- tempfile(fileext = ".html")
  tempfile_ <- tempfile_ %>% gt:::tidy_gsub("\\\\", "/")
  gt:::gt_save_html(data = data, filename = tempfile_, path = NULL)
  if (!requireNamespace("webshot2", quietly = TRUE)) {
    stop("The `webshot2` package is required for saving images of gt tables.", 
         call. = FALSE)
  }
  else {
    webshot2::webshot(url = paste0("file:///", tempfile_), 
                     file = filename, selector = "table", zoom = zoom, 
                     expand = expand, ...)
  }
}

new_gt_save(out,"w2.png")
1 Like

Thank you!
It worked.

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.