Produce Country flag using Emo::ji in R in gt table

678 views Asked by At

Output produced from code, still no flagI am trying to get country flags using library(emo) in R markdown document as part of gt table. Below is my code, I see ASCII code of countries rather than flag when I knit the document not sure what's wrong

    library(tidyverse)
    library(gt)
    library(emo)

 
   df <-data.frame(
      stringsAsFactors = FALSE,
                    Country = c("Health sciences",
                                "Physical sciences","Engineering",
                                "Computer science, maths","% of women inventores"),
                 Japan = c(0.24, 0.11, 0.11, 0.11, 0.08),
                 Chile = c(0.43, 0.23, 0.22, 0.16, 0.19),
        United.Kingdom = c(0.45, 0.21, 0.22, 0.21, 0.12),
         United.States = c(0.46, 0.2, 0.22, 0.22, 0.14),
                Mexico = c(0.46, 0.25, 0.26, 0.22, 0.18),
               Denmark = c(0.47, 0.22, 0.23, 0.18, 0.13),
                  EU28 = c(0.48, 0.25, 0.25, 0.22, 0.12),
                France = c(0.48, 0.24, 0.25, 0.22, 0.17),
                Canada = c(0.49, 0.21, 0.22, 0.22, 0.13),
             Australia = c(0.5, 0.23, 0.25, 0.24, 0.12),
                Brazil = c(0.57, 0.33, 0.32, 0.24, 0.19),
              Portugal = c(0.57, 0.37, 0.36, 0.27, 0.26)
         )

Code to produce gt table still not flag in output

df %>% 
  gt() %>% 
   cols_label(Country="Department" ,
              Japan=paste0("Japan",emo::ji("jp")),
              Chile=paste0(emo::ji("chile")),
              United.Kingdom=paste0(emo::ji("uk")),
              United.States=paste0(emo::ji("us")),
              Mexico=paste0(emo::ji("mexico")),
              Denmark=paste0(emo::ji("denmark")),
              EU28=paste0(emo::ji("eu")),
              France=paste0(emo::ji("france")),
              Canada=paste0(emo::ji("canada")),
              Australia=paste0(emo::ji("australia")),
              Brazil=paste0(emo::ji("brazil")),
              Portugal=paste0(emo::ji("portugal")))
1

There are 1 answers

0
Billy34 On

Maybe a bit overkill but works!

You can use URI encoded image in an html col label. For that you need to provision png icons for each country you need to display. Here I named them following their abbreviated name

library(base64enc)

flagAsIMG <- function(country_code) {
  img <- paste0("data:image/png;base64,", base64encode(paste0(country_code, ".png")))
  paste0("<img src='", img, "'/>")
}

library(gt)
library(magrittr)

gt(data.frame(japan=1)) %>%
  cols_label(japan=html("Japan", flagAsIMG("jp"))