Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R what does the domain argument in stop() function do?

When running function

stop("m<0", domain=NA)

the default setting of domain is NULL, but what happens when you set it to NA? I tried to change domain to that, but it didn't seem to do anything different. The error message reported is the same.

like image 435
Weiqing Wang Avatar asked Nov 08 '22 13:11

Weiqing Wang


1 Answers

In R, messages are divided into domains, and translations may be available for some or all messages in a domain. You can switch language domains by modifing the LANG variable:

> Sys.setenv(LANG = "fr")
> 2 + x
Erreur : objet 'x' introuvable
> Sys.setenv(LANG = "en")
> 2 + x
Error: object 'x' not found

Example Usage:

> Sys.setenv(LANG = "de")
> 
> if((length(nopkgs) > 0) && !missing(lib.loc)) {
+   pkglist <- paste(sQuote(nopkgs), collapse = ", ")
+   msg <- sprintf(ngettext(length(nopkgs),
+                           "library %s contains no packages",
+                           "libraries %s contain no packages",
+                           domain = "R-base"),
+                  pkglist)
+   warning(msg)
+ }
Fehler: Objekt 'nopkgs' nicht gefunden
> 

... Restart R session

> Sys.setenv(LANG = "en")
> if((length(nopkgs) > 0) && !missing(lib.loc)) {
+   pkglist <- paste(sQuote(nopkgs), collapse = ", ")
+   msg <- sprintf(ngettext(length(nopkgs),
+                           "library %s contains no packages",
+                           "libraries %s contain no packages",
+                           domain = "R-base"),
+                  pkglist)
+   warning(msg)
+ }
Error: object 'nopkgs' not found
> 

R makes use of the following domains.

  • Domain R for C-level error and warning messages from the R interpreter.
  • Domain R-pkg for the R stop and warning messages in each package, including R-base for the base package.
  • Domain pkg for the C-level messages in each package.
  • Domain RGui for the menus etc of the R for Windows GUI front-end.

Dividing up the messages in this way allows R to be extensible: as packages are loaded, their message translation catalogues can be loaded too.

Translations are looked for by domain according to the currently specified language, as specifically as possible, so, for example, an Austrian (de_AT) translation catalogue will be used in preference to a generic German one (de) for an Austrian user. However, if a specific translation catalogue exists but does not contain a translation, the less specific catalogues are consulted. For example, R has catalogues for en_GB that translate the Americanisms (e.g. gray) in the standard messages into English.

Translations in the right language but the wrong charset can generally be made use of by on-the-fly re-encoding. The LANGUAGE variable can be a colon-separated list, for example, se:de, giving a set of languages in decreasing order of preference.

If no suitable translation catalogue is found or a particular message is not translated in the selected catalogue, English is used. The translated catalogues are stored as binary files with extension .mo. Those for domains R and RGui are under R_HOME/share/locale and the package-specific catalogues under the po directory of the installed package (and so need to be under inst/po in the sources). These directories contain one directory for each translation identified by language, e.g. se or de_AT. Each language directory has a subdirectory LC_MESSAGES, and within that files for each domain. So an R installation will contain at least files

share/locale/en/LC_MESSAGES/R.mo
share/locale/en@quot/LC_MESSAGES/R.mo
library/splines/po/en/LC_MESSAGES/R-splines.mo
library/splines/po/en/LC_MESSAGES/splines.mo

(The `language' en@quot is English with Unicode bidirectional quotation marks for use in a UTF-8 locale.)

Ref: https://developer.r-project.org/Translations30.html

like image 61
Technophobe01 Avatar answered Nov 14 '22 21:11

Technophobe01