r - 如何将 UTF8 编码的表情符号字符转换为图像?

标签 r regex utf-8 emoji

我有一个包含表情符号的长向量,像这样:

emojis <- c("😐","😥","😴","😉","😛")

然后我有了他们的 utf-8 编码,我在 devtools::install_github( "ThinkRstat/utf8splain") 的帮助下提取了它,然后是 library(utf8splain)像这样,例如:(未准确表示)

emojis_enc <- c()
for(i in 1:length(emojis)){
  emojis_enc <- c(emojis_enc, utf8splain::runes(emojis[i])$rune)
}
#emojis_enc <- c("U+12345","U+67891","U+91234","U+56789","U+123A6")

然后我做了一个函数:

emojiPlot <- function(photo_enc, emo_char){
  png(paste0(photo_enc, ".png"))
  plot(emo_char, rescale = T, ylim = c(-1,1), xlim = c(-1,1))
  dev.off()
}

当我执行此操作时 emojiPlot("emojis_enc[1]", emojis[1]) 我只得到一个带有 x 轴和 y 轴的空图。我是 UTF 和 R 的新手。

我之所以这样做是因为我在gephi软件中使用了一个名为“imagepreview”的插件。并且该插件需要节点作为照片。所以我的目标是在 .png 中有单独的表情符号,并将它们的 utf 编码作为名称。因此,当我导入数据时,我只需将包含 utfs 的 CSV 指向照片的名称,然后拉出正确的表情符号。然后绘制它。

我正在为一个研究项目做这件事。我愿意接受更好的方法。

我在 Ubuntu 操作系统中。如果您使用 Windows,则表情符号将显示为正确的 utf 格式,并且不会被渲染。

最佳答案

获取表情符号的 png 文件的另一种解决方案是从 Twitter 下载它们,它接受链接到 png 文件的 url 中的 rune :

library(tidyverse)
data <- tibble(emojis = c("😐","😥","😴","😉","😛")) %>% 
  mutate(rune = map_chr(emojis, ~ utf8splain::runes(.)$rune)) %>%    # convert to runes
  mutate(rune = str_remove(rune, fixed("U+"))) %>%                   # remove leading U+
  mutate(emoji_url = paste0("https://abs.twimg.com/emoji/v2/72x72/", # make url
                            tolower(rune), ".png"))

# download the files
map2(data$emoji_url, paste0(data$rune, ".png"), function(x, y) download.file(x, y, method = "curl"))

这将下载 png 文件并将它们放在您的工作目录中。

在基地

tidyverse代码是可选的,同样可以在base中完成:

emojis <- c("😐","😥","😴","😉","😛")
rune <- sapply(emojis, function(x) utf8splain::runes(x)$rune)
emojiurl <- paste0("https://abs.twimg.com/emoji/v2/72x72/", tolower(rune), ".png")

for (i in seq_along(emojiurl)) {
  download.file(emojiurl[i], paste0(rune[i], ".png"), method = "curl")
}

关于r - 如何将 UTF8 编码的表情符号字符转换为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606453/

相关文章:

python - 在 igraph 的 InfoMap 实现中设置 vertex_weights

R:Plotly:树形图:仅对最低标签着色

java - 查找要在特定正则表达式中匹配的字符的索引

php - 在 PHP 中输​​出 UTF-8 数组的第一个字符

php - 将 latin1 编码的文本插入 utf8 表(忘记使用 mysql_set_charset)

r - 使用RSQLite时的R多行语句

r - 查找最大和最小矩阵边际总变异性

mysql - MYSQL 中的正则表达式错误

javascript - 如何替换 block /段落前后的字符?

html - 打开问号仅在某些 linux 环境中显示