20

I'm trying to write a function equivalent to scales::dollar that adds a pound (£) symbol to the beginning of a figure. Since the scales code is so robust, I've used it as a framework and simply replaced the $ for the £.

A stripped-down function example:

pounds<-function(x) paste0("£",x)

When I run a CHECK I get the following:

Found the following file with non-ASCII characters:
  pounds.R
Portable packages must use only ASCII characters in their R code,
except perhaps in comments.
Use \uxxxx escapes for other characters.

Looking through the Writing R extensions guide it doesn't give a lot of help (IMO) on how to resolve this issue. It mentions the \uxxxx and says it refers to Unicode characters.

Looking up unicode characters yields me the code &#163 but the guidance I can find for \uxxxx is minimal and relates to Java on W3schools.

My question is thus:

How do you implement the usage of non-unicode characters in R functions using the \uxxxx escapes and how does the usage affect the display of such characters after the function has been used?

bartektartanus
  • 15,284
  • 6
  • 74
  • 102
Steph Locke
  • 5,951
  • 4
  • 39
  • 77
  • For reference - see this question that is almost identical: http://stackoverflow.com/q/11452796/602276 – Andrie Feb 11 '15 at 12:12
  • Cheers @Andrie - not sure how I didn't find that in my googling the first time round. Karsten adds valuable information on how to identify what the unicode should be - I'd suggest marking as duplicate otherwise. – Steph Locke Feb 11 '15 at 12:17
  • Since this question also has answer, we can't close as duplicate. There is a process for merging similar questions, but I don't have time to do this. For me, it's good enough that the questions are linked. If somebody else wants to initiate the merging, that's fine too. – Andrie Feb 11 '15 at 12:19

3 Answers3

29

For the \uxxxx escapes, you need to know the hexadecimal number of your character. You can determine it using charToRaw:

sprintf("%X", as.integer(charToRaw("£")))
[1] "A3"

Now you can use this to specify your non-ascii character. Both \u00A3 and £ represent the same character.

Another option is to use stringi::stri_escape_unicode:

library(stringi)
stringi::stri_escape_unicode("➛")
# "\\u279b"

This informs you that "\u279b" represents the character "➛".

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
11

Try this:

pounds<-function(x) paste0("\u00A3",x)
bartektartanus
  • 15,284
  • 6
  • 74
  • 102
7

The stringi package can be useful is these situations:

library(stringi)

stri_escape_unicode("£")
#> [1] "\\u00a3"
Jot eN
  • 6,120
  • 4
  • 40
  • 59