0

Let's say that I have a character string containing bytes representing an emoji:

string <- "This is a test. U+1F600"

How can I transform it into

string <- "This is a test. \U0001F600"

So that I can render it as

utf8_print("This is a test \U0001F600")
[1] "This is a test ​"
Christopher Costello
  • 1,186
  • 2
  • 16
  • 30
  • Possible duplicate of https://stackoverflow.com/questions/29265172/print-unicode-character-string-in-r – Jason Jan 04 '18 at 23:40

1 Answers1

1

This is kind of a hack, but it works for your case:

string <- c("This is a test. U+1F600", "Another test")

# change U+XXXXYYYY to \UXXXXYYYY, quote and encode special characters
expr <- gsub("U[+]([0-9A-Fa-f]{1,8})", "\\\\U\\1",
             encodeString(string, quote = '"'))

# evaluate the string as an R expression
vapply(parse(text = expr, keep.source = FALSE), eval, "")
#> [1] "This is a test. \U0001f600" "Another test"
Patrick Perry
  • 1,422
  • 8
  • 17