Code Monkey home page Code Monkey logo

Comments (6)

MichaelChirico avatar MichaelChirico commented on June 10, 2024 1

I think utf8::utf8_print() is what I was after with "putting that logic into a package", let's add a reference to it in the doc there.

It's a good tool for,

OK, I've copy-pasted "👍" into a string in my package for users. Now it's time to submit to CRAN or otherwise run R CMD check, and I'm getting dinged for the non-ASCII characters -- how do I convert it to a \U string?

from rencfaq.

gaborcsardi avatar gaborcsardi commented on June 10, 2024 1

Oh, yeah, here it is:

withr::with_locale(c(LC_CTYPE = "C"), format("👷‍♂️"))
#> [1] "<U+0001F477><U+200D><U+2642><U+FE0F>"

from rencfaq.

gaborcsardi avatar gaborcsardi commented on June 10, 2024

Base R works with code points, so this works currently:

x <- "\U0001f477\u200d\u2642\ufe0f"
# https://apps.timwhitlock.info/unicode/inspect?s=%F0%9F%91%B7%E2%80%8D%E2%99%82%EF%B8%8F
x
#> [1] "👷‍♂️"

utf8::utf8_print(strsplit(x, "")[[1]], utf8 = FALSE)
#> [1] "\U0001f477" "\u200d"     "\u2642"     "\ufe0f"

Of course it would be more correct to work with graphemes, so if base R will switch to that, then it might not work any more.

Btw. cli also has now some handy functions for UTF-8 strings, e.g. it handles graphemes properly:

cli::utf8_nchar(x)
#> [1] 1

nchar(x)
#> [1] 4

from rencfaq.

gaborcsardi avatar gaborcsardi commented on June 10, 2024

Here is a base R solution:

Sys.setlocale("LC_ALL", "C")
#> [1] "C/C/C/C/C/en_US.UTF-8"

x
#> [1] "<U+0001F477><U+200D><U+2642><U+FE0F>"

It will mess up the current session of course...

from rencfaq.

MichaelChirico avatar MichaelChirico commented on June 10, 2024

Right... still useful to mention. For the use case mentioned, we can just open up a new process & run it there quickly. Nice!

from rencfaq.

gaborcsardi avatar gaborcsardi commented on June 10, 2024

Yeah, maybe there is a way to restore the locale, but withr::with_locale() refuses to change LC_ALL, and there might be a reason for that:

❯ withr::with_locale(c(LC_ALL = "C"), TRUE)
Error: Setting LC_ALL category not implemented.

callr can run it in another session:

❯ callr::r(function() { Sys.setlocale("LC_ALL", "C"); format("👷‍♂️") })
[1] "<U+0001F477><U+200D><U+2642><U+FE0F>"

Maybe it would be enough to change another category.

from rencfaq.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.