7

In my .bashrc, I have the line:

export SETTINGS=/home/user/settings.xml

If I load R in bash, I can access this variable using the Sys.getenv function:

Sys.getenv("SETTINGS")
"/home/user/settings.xml"

If I open up R in Emacs (M-x R), SETTINGS is empty:

Sys.getenv("SETTINGS")
""

What I have tried:

  1. adding the following to .emacs, based on How do I make Emacs recognize bash environment variables for compilation?

    ;; get environment vars from .bashrc
    (let ((path (shell-command-to-string ". ~/.bashrc; echo -n $SETTINGS")))
      (setenv "SETTINGS" path))
    
  2. opening up bash in emacs using M-x term

    echo $SETTINGS         # works
    R
    Sys.getenv("SETTINGS") #works
    
  3. If I open emacs from the terminal, the SETTINGS variable is available as expected. Opening emacs from the Applications menu (with either the command /usr/bin/emacs23 %F or emacs) does not work.

  4. comparing output from session("env") when loading R in bash vs emacs, but nothing stands out other than (bash = <, emacs = >):

    > INSIDE_EMACS=23.3.1,comint
    6d5
    < SETTINGS=/home/user/settings.xml
    9c8
    < SHLVL=1
    > SHLVL=0
    14a14
    > PAGER=cat
    16d15
    < PAGER=/usr/bin/pager
    19d17
    < COLORTERM=gnome-terminal
    25c23
    < WINDOWID=14680069
    > DESKTOP_AUTOSTART_ID=1020ce948b944a88113395253627645060000001863000
    < TERM=xterm
    > TERM=dumb
    

Can I either

  1. access SETTINGS from within R in emacs-ess
  2. export SETTINGS somewhere that I can access it?
Community
  • 1
  • 1
Abe
  • 12,956
  • 12
  • 51
  • 72
  • How are you starting Emacs? If you start a bash shell (with SETTINGS in the environment), and start Emacs from there, it should work. As it is, seems that the parent process does not have this environment variable (starting from Gnome shell or such?). – Matthew Lundberg Jun 13 '12 at 01:39
  • @MatthewLundberg I updated my answer (see point #3). Opening from the terminal works. Opening with a calls to `/usr/bin/emacs23 %F` from the Applications menu, Gnome-do, or a keyboard shortcut does not. – Abe Jun 13 '12 at 14:01

4 Answers4

3

I don't know about R and self-defined environment variables, but I set the PATH variable in emacs to the same value as in my bashrc. I modified my code to your problem, give it a shot and let me know if it works.

;; set env variable in Emacs
(getenv "SETTINGS")
(setenv "SETTINGS" "/home/user/settings.xml")

Original code (for PATH) is:

;; Emacs has its own path variable
(getenv "PATH")
 (setenv "PATH"
(concat
 "/usr/local/texlive/2011/bin/x86_64-linux" ":"
(getenv "PATH")))
elemakil
  • 3,681
  • 28
  • 53
3

the .bashrc might not be read when the xsession is started. Try adding

export SETTINGS=/home/user/settings.xml

in the .xsessionrc or the .gnomerc (if you are using gnome). Those file are loaded at startup for the X session.

Rémi
  • 8,172
  • 2
  • 27
  • 32
0

If you start you an R session inside emacs, then the R session will have the same environment variables as emacs. So you should first make sure that you start emacs in such a way that its environment contains your SETTINGS variable. That way, your R session, which you start inside emacs, will have SETTINGS also in its environment. How exactly to achieve this depends on your system.

ALiX
  • 1,021
  • 5
  • 9
  • My systems are Ubuntu and RedHat; do you have any further advice? How can I see the environment inside emacs? if I open up a bash terminal using `M-x term` in emacs, the variables are present. – Abe Jun 13 '12 at 13:49
0

You can put

SETTINGS <- "/home/user/settings.xml"

in .Rprofile as a work-around.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
Jason Morgan
  • 2,260
  • 21
  • 24
  • That would be a suitable workaround except that I want to be able to access the variable in "R --vanilla" mode – Abe Jun 13 '12 at 13:47