Skip to content

Instantly share code, notes, and snippets.

@jimjam-slam
Last active August 31, 2017 05:57
Show Gist options
  • Save jimjam-slam/e78c5c7b94e8cc66e4fe16b217a5922c to your computer and use it in GitHub Desktop.
Save jimjam-slam/e78c5c7b94e8cc66e4fe16b217a5922c to your computer and use it in GitHub Desktop.
Convert ISO 3166-1 alpha-2 country codes to their corresponding emoji flag control sequences (either as Unicode characters or ASCII strings). Great for use with the countrycode, emojifont and emoGG packages! Requires stringr (part of the tidyverse).
# these two functions, iso_to_emoji_unicode and iso_to_emoji_ascii, convert two-letter iso country codes
# (eg. australia = AU) to the unicode control sequences used for their flag emoji. the _unicode function
# outputs the actual unicode characters (printed in R in ascii but stored in ascii); the _ascii function
# outputs an ascii representation (with the argument ligature_sep in between the two strings).
library(stringr)
# iso_to_emoji_unicode: for a vector of two-letter iso codes, returns a
# corresponding vector of two-character unicode control sequences.
# (nb: R prints them in ascii, but they're really stored as unicode characters)
# great for use with countrycode and emojifont!
iso_to_emoji_unicode = function(iso_codes)
{
# check input
if (!any(
nchar(iso_codes) == 2 |
is.na(iso_codes)))
{
stop('iso_to_emoji: ISO codes must be two (2) letters long.')
}
if (!any(
str_detect(iso_codes, pattern = '[a-zA-Z][a-zA-Z]') |
is.na(iso_codes)))
{
stop('iso_to_emoji: ISO codes must be letters only.')
}
# substitute unicode regional indicator symbols for the original characters
return(str_replace_all(str_to_lower(iso_codes), c('a' = '๐Ÿ‡ฆ', 'b' = '๐Ÿ‡ง', 'c' = '๐Ÿ‡จ',
'd' = '๐Ÿ‡ฉ', 'e' = '๐Ÿ‡ช', 'f' = '๐Ÿ‡ซ', 'g' = '๐Ÿ‡ฌ', 'h' = '๐Ÿ‡ญ', 'i' = '๐Ÿ‡ฎ',
'j' = '๐Ÿ‡ฏ', 'k' = '๐Ÿ‡ฐ', 'l' = '๐Ÿ‡ฑ', 'm' = '๐Ÿ‡ฒ', 'n' = '๐Ÿ‡ณ', 'o' = '๐Ÿ‡ด',
'p' = '๐Ÿ‡ต', 'q' = '๐Ÿ‡ถ', 'r' = '๐Ÿ‡ท', 's' = '๐Ÿ‡ธ', 't' = '๐Ÿ‡น', 'u' = '๐Ÿ‡บ',
'v' = '๐Ÿ‡ป', 'w' = '๐Ÿ‡ผ', 'x' = '๐Ÿ‡ฝ', 'y' = '๐Ÿ‡พ', 'z' = '๐Ÿ‡ฟ')))
}
# iso_to_emoji_ascii: for a vector of two-letter iso codes, returns
# a corresponding vector of ascii-formatted unicode control sequences.
# great for downloading files named with unicode control points!
iso_to_emoji_ascii = function(iso_codes, ligature_sep = '-')
{
# check input
if (!any(
nchar(iso_codes) == 2 |
is.na(iso_codes)))
{
stop('iso_to_emoji: ISO codes must be two (2) letters long.')
}
if (!any(
str_detect(iso_codes, pattern = '[a-zA-Z][a-zA-Z]') |
is.na(iso_codes)))
{
stop('iso_to_emoji: ISO codes must be letters only.')
}
# add each letter's position in the alphabet to the start of the
# regional indicator symbol block; format as string
iso_codes %<>% str_to_lower
return(
paste0(
as.hexmode(0x1f1e5 + match(substr(iso_codes, 1, 1), letters)),
ligature_sep,
as.hexmode(0x1f1e5 + match(substr(iso_codes, 2, 2), letters))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment