tôi không thể nói chuyện với những gì đang xảy ra dưới mui xe liên quan đến chức năng cuộc gọi so với đối số chức năng, nhưng this email from Prof. Ripley từ năm 2008 có thể làm sáng tỏ (đoạn trích dưới đây):
R passes around, prints and plots UTF-8 character data pretty well, but it translates to the native encoding for almost all character-level manipulations (and not just on Windows). ?Encoding
spells out the exceptions [...]
lý do R không bản dịch này (trên Windows ít nhất) được đề cập trong the documentation that the OP linked to:
Windows has no UTF-8 locales, but rather expects to work with UCS-2 strings. R (being written in standard C) would not work internally with UCS-2 without extensive changes.
Các tài liệu R cho ?Quotes
giải thích cách bạn đôi khi có thể sử dụng ký tự out-of-locale nào (nhấn mạnh thêm):
Identifiers consist of a sequence of letters, digits, the period (.) and the underscore. They must not start with a digit nor underscore, nor with a period followed by a digit. Reserved words are not valid identifiers.
The definition of a letter depends on the current locale, but only ASCII digits are considered to be digits.
Such identifiers are also known as syntactic names and may be used directly in R code. Almost always, other names can be used provided they are quoted. The preferred quote is the backtick (`), and deparse will normally use it, but under many circumstances single or double quotes can be used (as a character constant will often be converted to a name). One place where backticks may be essential is to delimit variable names in formulae: see formula.
Có một cách khác để có được nhân vật như vậy, đó là sử dụng unicode trình tự thoát (như \u0394
cho Δ). Điều này thường là một ý tưởng tồi nếu bạn sử dụng ký tự đó cho bất kỳ điều gì khác ngoài văn bản trên một âm mưu (ví dụ, đừng làm điều này cho tên biến hoặc tên hàm, xem trích dẫn này từ the R 2.7 release notes, khi nhiều UTF-8 hiện tại hỗ trợ đã được thêm):
If a string presented to the parser contains a \uxxxx escape invalid in the current locale, the string is recorded in UTF-8 with the encoding declared. This is likely to throw an error if it is used later in the session, but it can be printed, and used for e.g. plotting on the windows() device. So "\u03b2" gives a Greek small beta and "\u2642" a 'male sign'. Such strings will be printed as e.g. <U+2642>
except in the Rgui console (see below).
Tôi nghĩ rằng điều này giải quyết hầu hết các câu hỏi của bạn, mặc dù tôi không biết tại sao có sự khác biệt giữa tên hàm và ví dụ đối số hàm bạn đã cung cấp; hy vọng ai đó có kiến thức nhiều hơn có thể kêu vang trong đó. FYI, trên Linux tất cả các cách khác nhau để gán và gọi một tác phẩm chức năng mà không có lỗi (vì hệ thống locale là UTF-8, vì vậy không dịch cần xảy ra):
Δ <- function(a,b) (a-b)/a # no error
`Δ` <- function(a,b) (a-b)/a # no error
"Δ" <- function(a,b) (a-b)/a # no error
"\u0394" <- function(a,b) (a-b)/a # no error
Δ(1:5, 9:13) # -8.00 -4.00 -2.67 -2.00 -1.60
`Δ`(1:5, 9:13) # same
"Δ"(1:5, 9:13) # same
"\u0394"(1:5, 9:13) # same
sessionInfo()
# R version 3.1.2 (2014-10-31)
# Platform: x86_64-pc-linux-gnu (64-bit)
# locale:
# LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
# LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
# LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
# LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
# attached base packages:
# stats graphics grDevices utils datasets methods base
Trong R-internals: gì Người dùng R nghĩ là biến hoặc đối tượng là biểu tượng được gắn với giá trị. Tôi nghĩ phần CHARSXP bạn liên kết là giá trị và bạn thực sự quan tâm đến các quy tắc cho biểu tượng. Điều đó nói rằng, tôi đã làm việc trên mã R viết bằng tiếng Trung, vì vậy tôi mong đợi delta làm việc. –
Bạn đang sử dụng phiên bản R nào/hệ điều hành/ngôn ngữ gì? Tôi nhận được "Lỗi: \ uxxxx chuỗi không được hỗ trợ bên trong backticks (dòng 1)" khi gán một chức năng để 'Δ' (Thử nghiệm trên ngày hôm nay của R-devel và 3.1.0 theo Win 7, tiếng Anh Vương quốc Anh.) –
Phiên bản nào của R bạn đang sử dụng 'Δ <- hàm (a, b) (ab)/a' nào đó không thành công? Và khi bạn nói nó "thất bại", bạn có ý gì? Bạn có gặp lỗi cú pháp không? Nếu làm việc cho tôi trên phiên bản 'R phiên bản 3.1.0, x86_64-apple-darwin10.8.0 (64 bit) 'locale en_US.UTF-8 – MrFlick