如何在R包函数中使用非ASCII符号(例如£)?

And*_*rie 13 warnings ascii r package

我在我的一个R包中有一个简单的函数,其中一个参数symbol = "£":

formatPound <- function(x, digits = 2, nsmall = 2, symbol = "£"){ 
  paste(symbol, format(x, digits = digits, nsmall = nsmall)) 
}
Run Code Online (Sandbox Code Playgroud)

但是在运行时R CMD check,我收到了这个警告:

* checking R files for non-ASCII characters ... WARNING
Found the following files with non-ASCII characters:
  formatters.R
Run Code Online (Sandbox Code Playgroud)

这绝对是£导致问题的符号.如果我用合法的ASCII字符代替它$,警告就会消失.

问题:如何£在我的函数参数中使用,而不会R CMD check发出警告?

Dir*_*tel 13

看起来"编写R扩展"在第1.7.1节"编码问题"中介绍了这一点.


此页面中的一个建议是使用Unicode编码\uxxxx.由于£是Unicode 00A3,您可以使用:

formatPound <- function(x, digits=2, nsmall=2, symbol="\u00A3"){
  paste(symbol, format(x, digits=digits, nsmall=nsmall))
}


formatPound(123.45)
[1] "£ 123.45"
Run Code Online (Sandbox Code Playgroud)


Vil*_*mko 5

作为解决方法,您可以使用intToUtf8()函数:

# this causes errors (non-ASCII chars)
f <- function(symbol = "?")

# this also causes errors in Rd files (non-ASCII chars)
f <- function(symbol = "\u279B")

# this is ok
f <- function(symbol = intToUtf8(0x279B))
Run Code Online (Sandbox Code Playgroud)