Christopher Costello
Christopher Costello

Reputation: 1266

R - Emoji unicode to character

I'm coding in R. Let's say that I have the unicode value of an emoji as

wink_emoji <- "\U0001f609"

Or, alternatively, as

wink_emoji <- "U+1f609"

Is there a function that I can use to print the actual emoji character to the console? Like this:

[1] "😉"

How about as its HTML character entity?

[1] "&#128521;"

Furthermore, if I have a string like so:

test <- "This is a test U+1f609 U+1F469 U+200D U+2764 U+FE0F U+200D U+1F48B U+200D U+1F469"

Can I run it through a function to match all of the emojis and return this:

[1] "This is a test 😉👩‍❤️‍💋‍👩"

Upvotes: 4

Views: 3726

Answers (1)

Patrick Perry
Patrick Perry

Reputation: 1482

Use utf8_print from the utf8 package to print, utf8ToInt to get the integer value of the code point:

wink_emoji <- "\U0001f609"
utf8::utf8_print(wink_emoji)
#> [1] "😉​"
utf8ToInt(wink_emoji)
#> [1] 128521

(Printing emoji only works on MacOS and Linux, not on Windows.)

Upvotes: 3

Related Questions