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

2

8

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?)

Trinitrotoluene answered 1/10, 2013 at 21:35 Comment(0)
J
6

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.

Joannejoannes answered 28/10, 2013 at 17:12 Comment(9)
thanks for the hints, but I still can't see how to get this to work. I have 'module load R/3.0.1' in both my .profile and .bashrc but it doesn't seem to helpTrinitrotoluene
Did you try adding the debugging messages to .profile, .bash_profile, and/or .bashrc and then checking the *debug tramp/ssh..* buffer to see that the files are executed after the connection is established?Joannejoannes
By debugging message, do you mean adding a line like "echo 'bashrc loaded"?Trinitrotoluene
yes, and similar messages to other files to see which one(s) gets loaded.Joannejoannes
okay. So it is reading my .bashrc, but when I open R, it is ignoring the command module load R/3.0.1, although this line works as expected when launching R on the remote machine from the bash shellTrinitrotoluene
module is supposed to modify the environment. If it doesn't work under TRAMP for some reason, you can try adding the path to correct R: (add-to-list 'tramp-remote-path "/path/to/R-3.0.1/bin")Joannejoannes
I have just added an alternative method to use correct R that uses your working ssh configuration and doesn't rely on TRAMP.Joannejoannes
This answer saved me from having to ask a similar question, thanks!Barrel
By default, TRAMP will not use the PATH constructed by .profile unless you explicitly tell it to do so. See this answer for a solution.Wanids
A
1

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)
Artisan answered 14/12, 2017 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.