如何在 R 包函数中使用非 ASCII 符号(例如 £)? [英] How to use a non-ASCII symbol (e.g. £) in an R package function?

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

问题描述

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

I have a simple function in one of my R packages, with one of the arguments symbol = "£":

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

但是在运行 R CMD check 时,我收到以下警告:

But when running R CMD check, I get this warning:

* checking R files for non-ASCII characters ... WARNING
Found the following files with non-ASCII characters:
  formatters.R

肯定是那个 £ 符号导致了问题.如果我将其替换为合法的 ASCII 字符,例如 $,警告就会消失.

It's definitely that £ symbol that causes the problem. If I replace it with a legitimate ASCII character, like $, the warning disappears.

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

Question: How can I use £ in my function argument, without incurring a R CMD check warning?

推荐答案

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

Looks like "Writing R Extensions" covers this in Section 1.7.1 "Encoding Issues".

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

One of the recommendations in this page is to use the Unicode encoding uxxxx. Since £ is Unicode 00A3, you can use:

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


formatPound(123.45)
[1] "£ 123.45"

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

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