在函数内部使用非ASCII字符

Ste*_*cke 12 string unicode ascii r

我正在尝试编写一个等效的函数scales::dollar,在图的开头添加一个英镑(£)符号.由于比例代码非常强大,我将它用作框架并简单地将$替换为£.

一个精简的功能示例:

pounds<-function(x) paste0("£",x)
Run Code Online (Sandbox Code Playgroud)

当我运行CHECK时,我得到以下内容:

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.
Run Code Online (Sandbox Code Playgroud)

通过编写R扩展指南,它没有提供很多帮助(IMO)如何解决此问题.它提到\ uxxxx并说它引用了Unicode字符.

查找unicode字符会产生代码,&#163但我能找到的指导\uxxxx很少,并且与W3schools上的Java有关.

我的问题是:

如何使用\ uxxxx转义在R函数中实现非unicode字符的使用?在使用该函数后,使用情况如何影响这些字符的显示?

Kar*_* W. 13

对于\ uxxxx转义,您需要知道角色的十六进制数.您可以使用charToRaw以下方法确定

sprintf("%X", as.integer(charToRaw("£")))
[1] "A3"
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用它来指定非ascii字符.双方\u00A3£表示相同的字符.


bar*_*nus 10

试试这个:

pounds<-function(x) paste0("\u00A3",x)
Run Code Online (Sandbox Code Playgroud)


Jot*_* eN 5

stringi包可能是有用的是这些情况:

library(stringi)

stri_escape_unicode("£")
#> [1] "\\u00a3"
Run Code Online (Sandbox Code Playgroud)