问题
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 😀"
回答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"
来源:https://stackoverflow.com/questions/48105103/coverting-unicode-codeepoint-format