How to use a non-ASCII symbol (e.g. £) in an R package function?

后端 未结 2 1858
后悔当初
后悔当初 2020-12-17 17:21

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

formatPound <- function(x, digits = 2, nsmall = 2         


        
相关标签:
2条回答
  • 2020-12-17 17:49

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


    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"
    
    0 讨论(0)
  • 2020-12-17 17:49

    As a workaround, you can use intToUtf8() function:

    # 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))
    
    0 讨论(0)
提交回复
热议问题