Skip to content

Wrong locale when using R from shell

6 messages · Erich Studerus, Simon Urbanek, Marc Schwartz

#
Hello

I'm using Lyx with Sweave on Mac OS X 10.5.8 with R version 2.10.1. When I 
compile Lyx documents, R is called from shell to process R code chunks 
contained in the Lyx document. Unfortunately, R does not automatically 
detect the correct locale when called from shell. I figured out that R uses 
the C locale in shell mode by embedding the following code in Lyx

<<echo=F>>=
sys.getlocale()
@

When I start R from the terminal and also in the R.app I get the correct 
locale:
de_CH.UTF-8/de_CH.UTF-8/C/C/de_CH.UTF-8/de_UTF-8

How I can I get the correct locale when calling R from shell?

Regards,
Erich
#
On Mar 22, 2010, at 16:32 , Erich Studerus wrote:

            
Setup your environment accordingly? This has nothing to do with R but  
your environment (LANG, LANGUAGE, LC_xxx) - which is what determines  
the locale. The default locale settings are system-dependent, so  
either your system default locale is C or Lyx forces C locale for the  
shell it starts. For details on locale specifications see, e.g.:
http://www.gnu.org/software/hello/manual/gettext/Locale-Environment-Variables.html#Locale-Environment-Variables
http://opengroup.org/onlinepubs/007908799/xbd/envvar.html

Cheers,
Simon
#
I typed "export" into the mac terminal to see the setup of my system 
variables. Under language I get LANG="de_CH.UTF-8". So it seems my system 
variable for language is set up correctly.

It doesn't make sense that Lyx would force C locale, although I don't know 
where I can check this. I'm using the regular Lyx/Sweave setup described 
here:
http://wiki.lyx.org/LyX/LyxWithRThroughSweave

I dont have this locale problem on my Windows 7 computer. I don't 
understand, why R can dectect the correct locale when started from the 
terminal or as R.app but not from the shell. How do these running modes 
differently look up environmental variables?

Regards,
Erich

On Mon, 22 Mar 2010 17:09:21 -0400
Simon Urbanek <simon.urbanek at r-project.org> wrote:
#
Erich,

In looking at the files listed on the web site you reference, the preferences file for OSX (http://cran.r-project.org/contrib/extra/lyx/preferences) would seem to set up the execution associations for various calls to R while within LyX.

It is not clear to me why the locale is not being picked up, but there may be some interaction with LyX and the shell environment when R is executed by the commands listed in the preferences file, presuming that the converters section defines this exec mechanism. LyX is built on the QT library (primarily used under KDE on Linux), ported to OSX and there may be other issues at play.

Thus, if my assumptions are correct, you might want to modify each of the "R CMD ..." sequences to:

  "export LANG="de_CH.UTF-8"; R CMD ..."

and see if that helps.

BTW, on some of the LyX OSX related pages, there are references to the use of ~/.MacOSX/environment.plist to set certain environment variables. The use of this approach is known to conflict with certain applications and I would recommend avoiding the use of this file, especially on Snow Leopard if you have upgraded.

HTH,

Marc Schwartz
On Mar 22, 2010, at 5:06 PM, Erich Studerus wrote:

            
#
On Mar 22, 2010, at 6:06 PM, Erich Studerus wrote:

            
Nope ;). I assume you're looking in Terminal which sets the LANG variable according to its preferences - it is NOT set by the shell.
Both of them set LANG explicitly - it's that easy. Apparently LyX doesn't. Again, you're barking up the wrong tree - it's not R's fault since R gives you what you ask for - namely the C locale. If LyX uses a shell to run R, it should be as easy as replacing "R" with "LANG=en_US.UTF-8 R" or whichever you prefer. I don't use LyX so I don't know what where exactly you have to change it. Note that the locale is NOT changed in the system-wide startup scripts (by default).

Cheers,
Simon
#
Thank you very much! Replacing "R" with "LANG=de_CH.UTF-8 R" in the Lyx 
preference file indeed solved my problem.

On Mon, 22 Mar 2010 19:15:51 -0400
Simon Urbanek <simon.urbanek at r-project.org> wrote: