Reputation: 1547
I'm trying to make Shiny print text output that includes emoji. Unfortunately, it seems that rather than printing the emoji, it prints the Unicode numbers of the different symbols, like so:
Here is a simple example of Shiny not handling emoji. it loads three emoji from an emoji.json
file:
{"emoji":["😅","😒","😫"]}
And simply renders the text in app.R
library(jsonlite)
library(shiny)
library(tidyverse)
ui <- fluidPage(textOutput("emoji"))
server <- function(input, output) {
output[["emoji"]] <- renderText({
"emoji.json" %>%
read_json() %>%
.[["emoji"]] %>%
str_c(collapse="")
})
}
shinyApp(ui = ui, server = server)
It feels like there must be some transformation I need to do somewhere, but I can't tell what. Also this is being done on a Windows 10 computer using R version 3.5.0. Thanks!
Upvotes: 4
Views: 1833
Reputation: 12165
I think this depends on the encoding of your emoji.json
file. When I copied your example file into TextEdit (I'm on a Mac), converted it to plain text, and saved it as a .json
with UTF-8
encoding, the emojis displayed correctly both in TextEdit and in the shiny app you posted.
I though the problem might be due to encoding, so I replaced the emojis with their hex equivalents and saved the file with Western (Windows Latin 1)
encoding so they appeared as plain text of hex numbers.
Getting them to display properly in Shiny simply required combining as.integer
to convert from Hex and intToUtf8
to render them as UTF8
characters.
JSON file with Hex representations of the emoji in Western (Windows Latin 1)
encoding:
{"emoji":["0x0001F605","0x0001F612","0x0001F62B"]}
Shiny app:
library(jsonlite)
library(shiny)
library(tidyverse)
ui <- fluidPage(textOutput("emoji"))
server <- function(input, output) {
output[["emoji"]] <- renderText({
"emoji3.json" %>%
read_json() %>%
.[["emoji"]] %>%
sapply(function(x) {
intToUtf8(as.integer(x))
})
})
}
shinyApp(ui = ui, server = server)
The Emoji are properly displayed:
Upvotes: 1