Flux
Flux

Reputation: 10920

How do I set R console's language to English using a configuration file?

I am trying to set my R console's language to English using a configuration file. I am using Debian and Ubuntu as my OS. To set the R console's language to English, I created ~/.Renviron containing:

LANGUAGE = 'en_US.UTF-8'
LC_ALL = 'en_US.UTF-8'

This works, but I only found it through trial and error, so I am concerned that this will break if I change my system's default languages and locales in the future. These are my current system's environment variables:

$ env | grep "LANG\|LC_"                                                           
LANG=fr_FR.UTF-8
LANGUAGE=fr_FR.UTF-8:zh_CN.UTF-8:en_US.UTF-8
LC_ADDRESS=en_SG.UTF-8
LC_IDENTIFICATION=en_SG.UTF-8
LC_MONETARY=en_SG.UTF-8
LC_MEASUREMENT=en_SG.UTF-8
LC_NAME=en_SG.UTF-8
LC_NUMERIC=en_SG.UTF-8
LC_PAPER=en_SG.UTF-8
LC_TELEPHONE=en_SG.UTF-8
LC_TIME=en_SG.UTF-8

I want to make sure that no matter what my future system's LANG, LANGUAGE, LC_* are going to be, the values in ~/.Renviron will ensure that the R console's language is English. How can I do that? Does my current ~/.Renviron achieve this goal?

In other words, is setting LANGUAGE and LC_ALL to en_US.UTF-8 in ~/.Renviron sufficient to guarantee that the R console's language is always English no matter what my system's default languages and locales become in the future?

I've read this: How to change language settings in R, but the answers there do not use a configuration file.

Upvotes: 0

Views: 642

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545548

Does my current ~/.Renviron achieve this goal?

Probably yes, but potentially not quite.

The relevant information can be found in the locales documentation:

The following categories should always be supported: "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC" and "LC_TIME". Some systems (not Windows) will also support "LC_MESSAGES", "LC_PAPER" and "LC_MEASUREMENT". […]

Note that setting category "LC_ALL" sets only categories "LC_COLLATE", "LC_CTYPE", "LC_MONETARY" and "LC_TIME". […]

Note that the LANGUAGE environment variable has precedence over "LC_MESSAGES" in selecting the language for message translation on most R platforms.

So you might want to also set those categories not set by LC_ALL or LANGUAGE:

  • LC_NUMERIC
  • LC_PAPER
  • LC_MEASUREMENT

Lastly, the R “Startup” documentation tells us that using ~/.Renviron is a good place to set these:

Unless --no-environ was given on the command line, R searches for site and user files to process for setting environment variables. […] The name of the user file can be specified by the R_ENVIRON_USER environment variable; if this is unset, the files searched for are ‘.Renviron’ in the current or in the user's home directory (in that order).

Personally I prefer de-cluttering my home directory and putting all such configuration under ~/.config, e.g. ~/.config/R/REnviron. Doing so requires slightly more work, however, since R by default doesn’t respect the XDG conventions: to fix this I’m setting the environment variables R_ENVIRON_USER, R_LIBS_USER and R_PROFILE_USER in my .bashrc:

export R_ENVIRON_USER=$HOME/.config/R/Renviron

# Need to be set here rather than in REnviron so that they can be overridden
# temporarily:

export R_PROFILE_USER=${XDG_CONFIG_HOME-$HOME/.config}/R/init.r
export R_LIBS_USER=${XDG_DATA_HOME-$HOME/.local/share}/R/x86_64-pc-linux-gnu-library/%v

Upvotes: 2

Related Questions