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
Hi!
To help us help you, could you please prepare a repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
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)
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
system
Closed
June 23, 2022, 7:57pm
8
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.