Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unicode symbols (emoji flags) #225

Merged
merged 1 commit into from May 11, 2020
Merged

Add Unicode symbols (emoji flags) #225

merged 1 commit into from May 11, 2020

Conversation

NilsEnevoldsen
Copy link
Contributor

@NilsEnevoldsen NilsEnevoldsen commented May 11, 2020

This adds Unicode region subtags as pairs of Regional Indicator Symbols. Region subtags represent regions, not flags. When composed of Regional Indicator Symbols, they are displayed as flags on most systems, but on Windows they are displayed as pairs of letters.

https://en.wikipedia.org/wiki/Regional_Indicator_Symbol

Unicode lists valid Unicode region subtags. They are based on ISO 3166-1 alpha-2.

https://unicode.org/reports/tr51/#Flags

> countrycode('Antarctica','country.name','unicode.symbol')
[1] "\U0001f1e6\U0001f1f6"
> countrycode('Antarctica','country.name','unicode.symbol') %>% utf8::utf8_print()
[1] "πŸ‡¦πŸ‡Άβ€‹"
> countrycode('πŸ‡¦πŸ‡Ά','unicode.symbol','country.name')
[1] "Antarctica"

@NilsEnevoldsen
Copy link
Contributor Author

Fixes #224

@cjyetman
Copy link
Collaborator

Ha! This is cool. Thanks @NilsEnevoldsen!

cat(countrycode('Germany', 'country.name', 'unicode.symbol'))
#> πŸ‡©πŸ‡ͺ

@vincentarelbundock
Copy link
Owner

Yeah, this is very cool!

I think it might be nice to add this to the README because it's a cute "showy" feature. Does anyone know how to render flags in a plot? I'm thinking of something like this:

library(countrycode)
countries <- c('Canada', 'Germany', 'Thailand')
flags <- countrycode(countries, 'country.name', 'unicode.symbol')
barplot(1:3, names.arg = flags)

But this only shows me emptys squares instead of the flags.

@vincentarelbundock vincentarelbundock merged commit 06e9b01 into vincentarelbundock:master May 11, 2020
@NilsEnevoldsen
Copy link
Contributor Author

R graphics devices are pretty bad with Unicode, so I have mixed feelings about talking this feature up too much. If you export the graph to an SVG and display the graph with, say, a web browser, it should work better. Maybe Unicode fonts work better with Shiny? (I don't know; I haven't used Shiny much.)

library(ggplot2)
library(gridSVG)
ggplot() + annotate("text", x = 0, y = 0, label = "πŸ‡¦πŸ‡Ά", size = 32)
grid.export("emoji.svg")

Even Unicode on the command prompt can depend on the OS. Here's what it looks like for me on macOS:

> countrycode('Antarctica','country.name','unicode.symbol')
[1] "\U0001f1e6\U0001f1f6"
> countrycode('Antarctica','country.name','unicode.symbol') %>% utf8::utf8_print()
[1] "πŸ‡¦πŸ‡Άβ€‹"

@NilsEnevoldsen
Copy link
Contributor Author

@vincentarelbundock To modify your example,

countries <- c('Canada', 'Germany', 'Thailand')
flags <- countrycode::countrycode(countries, 'country.name', 'unicode.symbol')
barplot(1:3, names.arg = flags)
gridGraphics::grid.echo()
gridSVG::grid.export("barplot.svg")

@vincentarelbundock
Copy link
Owner

Thanks @NilsEnevoldsen . I agree that this is too complicated and system-dependent for us to offer a comprehensive solution. I still think the feature is really cool, so I included a section to the README where I showcase it in a gt table. Since that's HTML output, everything looks great:

https://github.com/vincentarelbundock/countrycode#unicode-flags

Thanks again!

@NilsEnevoldsen
Copy link
Contributor Author

Excellent compromise. It does add a bit of color to the docs.

Note that embedding unicode characters in R graphics is possible, but it can be tricky. We won't be able to offer support with that. Good luck!

Seems reasonable.

@NilsEnevoldsen
Copy link
Contributor Author

@vincentarelbundock Oh, if you wanted to, you could add the advice that if your output looks like "\U0001f1e6\U0001f1f6" then you can try piping the output to %>% utf8::utf8_print(). That will cover a lot of cases without dipping into the complexity of graphics devices.

As a rule of thumb, if your output looks like β–‘β–‘β–‘β–‘ (boxes) then you're in trouble, but if it looks like "\U0001f1e6\U0001f1f6" then there might be a simple solution.

Up to you whether you think that's worthwhile.

@vincentarelbundock
Copy link
Owner

good idea. done.

@NilsEnevoldsen NilsEnevoldsen mentioned this pull request May 12, 2020
@NilsEnevoldsen NilsEnevoldsen deleted the emojiflags branch May 16, 2020 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants