在包函数中使用非ASCII字符 [英] Using non-ASCII characters inside functions for packages

查看:109
本文介绍了在包函数中使用非ASCII字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个等效于scales::dollar的函数,该函数在图形的开头添加了磅(£)符号.由于标度代码非常健壮,因此我将其用作框架,只是将$替换为£.

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 £.

一个简化的功能示例:

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

运行CHECK时,我得到以下信息:

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.

仔细阅读 Writing R扩展指南,对于如何解决此问题并没有太多帮助(IMO).它提到\ uxxxx并说它是指Unicode字符.

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.

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

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.

因此,我的问题是:

如何在R函数中使用\ uxxxx转义符实现非Unicode字符的用法,以及使用该函数后如何影响此类字符的显示?

推荐答案

对于\ uxxxx转义,您需要知道字符的十六进制数.您可以使用charToRaw来确定它:

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"

现在,您可以使用它来指定您的非ASCII字符. \u00A3£代表相同的字符.

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

另一种选择是使用stringi::stri_escape_unicode:

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

这会通知您"\u279b"代表字符"➛".

这篇关于在包函数中使用非ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆