Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a server's specific R installation (environment module) when launching a local installation of emacs?

I am using a cluster with environment modules. This means that I must specifically load any R version other than the default (2.13) so to load R 3.0.1, I have to specify

module load R/3.0.1
R

I have added module load R/3.0.1 to .bashrc, so that if I ssh into the server and load R, it opens 3.0.1 by default. But when I open R on the server (M-x R, starting data directory: /ssh:myserver), it loads the default R installation (2.13).

This question is similar to previous questions except that I am accessing R on a server using a local installation of emacs. (ESS to call different installations of R and How can I specify the R version opened by ESS session in emacs?)

like image 725
David LeBauer Avatar asked Oct 01 '13 21:10

David LeBauer


2 Answers

TL;DR

  1. Identify path of R module: module show R/3.0.1
  2. add to your .emacs: (add-to-list 'tramp-remote-path "/path/to/R-3.0.1/bin")

bash called by TRAMP via ssh does execute its initialization files but which files ultimately get executed depends on several things. You can check if your ~/.bashrc gets executed at all when you use TRAMP to connect to the server by adding something like echo "bashrc here" to the file. Then set variable

(setq tramp-verbose 9)

and try connecting. Now see if you can spot that message in a buffer called *debug tramp/ssh...*.

If you don't have any additional settings, TRAMP calls remote shell as /bin/sh (http://www.gnu.org/software/tramp/#Remote-shell-setup) so bash will execute /etc/profile and ~/.profile (http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files). You can also see how the shell is called in that *debug tramp/ssh...* buffer, the relevant line will be

tramp-open-shell (5) # Opening remote shell `/bin/sh'...done

Try adding the module load R/3.0.1 to ~/.profile first. If that does not help, try forcing TRAMP to call bash by its proper name by setting

(setq explicit-shell-file-name "bash")
(setq shell-file-name "bash")

UPDATE: If all else fails, you can just open shell M-x shell, then ssh to you server which should take care of the proper modules initialization (because you mentioned that interactive ssh connection works as expected). Once on the server, launch R and then do M-x ess-remote to make ESS aware of existing R session. This way you don't rely on TRAMP at all.

like image 111
Alex Vorobiev Avatar answered Nov 20 '22 20:11

Alex Vorobiev


When TRAMP runs a shell command on the remote host, it calls /bin/sh -c. There doesn't seem to be a way to tell sh to source any files at initialization when it is called like that. So let's instead configure TRAMP to call /bin/bash -c. Then bash will source BASH_ENV, which we can point at a custom file that configures the modules.

So, first, configure TRAMP to use /bin/bash. To do this, we need to modify the tramp-methods variable. It's an alist, where the keys are strings denoting the connection type. I use the "scpx" connection type, but you can change that to a whichever connection type you use.

(let ((scpx-method (cdr (assoc "scpx" tramp-methods))))
      (add-to-list 'scpx-method '(tramp-remote-shell "/bin/bash"))
      (add-to-list 'tramp-methods (cons "scpx" scpx-method)))

Then, we can configure tramp-remote-process-environment to point at a file that will contain our module configuration.

(add-to-list 'tramp-remote-process-environment "BASH_ENV=~/.bash_env")

Then, open up the ~/.bash_env file on the remote machine. You'll need to source the files that set up your module system. We use a different module system, so I'm not entirely sure what file you'll need, but perhaps you'll find it in /etc/profile.d. Here's what my file contains:

source /etc/profile.d/z00_lmod.sh
module -q restore

Again, I'm not familiar with your module system, but that second line simply loads up my default set of modules.

Lastly, since the module system configures your PATH, we need to get TRAMP to use it. By default, TRAMP just uses the contents of tramp-remote-path. But if you add tramp-own-remote-path, it will pull in the contents of PATH.

(add-to-list 'tramp-remote-path 'tramp-own-remote-path)
like image 36
Daniel Matz Avatar answered Nov 20 '22 20:11

Daniel Matz