How to access bash environment variable from R in emacs-ess

In my .bashrc, I have a 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 R in Emacs (Mx R), SETTINGS is empty:

 Sys.getenv("SETTINGS") "" 

What I tried:

  • adding the following to .emacs based How do I get Emacs to recognize bash environment variables for compilation?

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

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

  • comparing the output of session("env") when loading R in bash vs emacs, but nothing stands out except (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

  • Access settings from R to emacs-ess
  • export SETTINGS anywhere that I can access?
+6
source share
4 answers

I do not 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 changed my code to your problem, take a snapshot and let me know if it works.

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

Source code (for PATH):

 ;; Emacs has its own path variable (getenv "PATH") (setenv "PATH" (concat "/usr/local/texlive/2011/bin/x86_64-linux" ":" (getenv "PATH"))) 
+3
source

. bashrc may not be read when xsession starts. Try to add

 export SETTINGS=/home/user/settings.xml 

in .xsessionrc or .gnomerc (if you are using gnome). These files are loaded at startup for session X.

+3
source

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

0
source

You can put

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

in .Rprofile as a workaround.

0
source

Source: https://habr.com/ru/post/917918/


All Articles