13

I want to run R on a remote box under a local Emacs (I do not want to run Emacs on the remote box).

I can run R on a remote host using TRAMP:

(let ((default-directory "/user@remote:~"))
  (R))

and everything works fine except that when the connection to remote is lost, R dies. This is no good because this means that I have to re-load all the data into R after restarting it, which takes time.

Is it possible to tell TRAMP to use a persistent terminal? (GNU Screen or tmux or Mosh or dtach)

See also emacs-devel thread tramp:sshx:(screen|tmux).

sds
  • 58,617
  • 29
  • 161
  • 278

2 Answers2

10

Here is an alternative approach using dtach:

(defvar R-remote-host "remote-server")
(defvar R-remote-session "R")
(defvar R-remote-directory "~")
(defun R-remote (&optional remote-host session directory)
  "Connect to the remote-host's dtach session running R."
  (interactive (list
                (read-from-minibuffer "R remote host: " R-remote-host)
                (read-from-minibuffer "R remote session: " R-remote-session)
                (read-from-minibuffer "R remote directory: " R-remote-directory)))
  (pop-to-buffer (make-comint (concat "remote-" session)
                              "ssh" nil "-t" "-t" remote-host
                              "cd" directory ";"
                              "dtach" "-A" (concat ".dtach-" session)
                              "-z" "-E" "-r" "none"
                              inferior-R-program-name "--no-readline"
                              inferior-R-args))
  (ess-remote (process-name (get-buffer-process (current-buffer))) "R")
  (setq comint-process-echoes t))

Call M-x R-remote RET RET RET RET.

It works for me.

PS. The answer to the problem (as opposed to the question as asked) is to use ein with Jupyter.

sds
  • 58,617
  • 29
  • 161
  • 278
  • Thanks for your code. Right now when starting a remote process, the process starts in the home directory. I tried to modify your code open R remotely in a specific directory but had some problem achieving this. Do you have any way of choosing the directory where the process should start? – DJJ Dec 14 '15 at 13:43
  • Ok I got it just adding the line `"cd ~/Documents/R;"` after remote host does the job. Many thanks again for your code it saved me a lot of trouble. – DJJ Dec 14 '15 at 13:48
  • @DJJ: I added `R-remote-directory`, thanks for the suggestion. – sds Dec 14 '15 at 13:58
9

Here is how to use ESS with R running in a remote screen session:

  • ssh to the remote host (outside of emacs)

  • start screen session

  • detach it

  • open shell in emacs (M-x shell)

  • ssh to the remote host again in the emacs shell

  • resume the screen session (screen -r)

  • start R

  • finally attach ESS to the R process using M-x ess-remote in the shell buffer where you started R

There are more details, screenshots, and keybindings in this post http://blog.nguyenvq.com/2010/07/11/using-r-ess-remote-with-screen-in-emacs/

Alex Vorobiev
  • 4,349
  • 21
  • 29
  • ESS developers seem to consider `ess-remote` to be obsolete. – sds May 29 '13 at 17:39
  • Well, the function itself is rather simple, all it does is sets a few variables and initializes inferior-ess mode for the buffer plus some language-specific customizations. I use ess-remote a lot to run remote R and SAS processes and have not had any problems with the function. You can also use it with R launched ssh.el – Alex Vorobiev May 29 '13 at 17:59
  • 2
    @sds As ESS developer I can assure that ess-remote is maintaned and is improving. I am looking into screen integration with ess and tramp/ess-remote. Any ideas suggestions are welcome. – VitoshKa Nov 01 '13 at 20:20
  • Um, this doesn't work. Emacs shell sets your TERM to dumb. When you ssh to the remote host, you find that screen doesn't support your terminal setting. "Clear screen capability required." – dk. Feb 03 '14 at 16:35
  • Try setting `TERM=xterm` after you launch shell in emacs, then ssh, etc. – Alex Vorobiev Feb 04 '14 at 14:55
  • You could always use the Emacs terminal (`M-x ansi-term`) to run your shell, instead of `M-x shell`. If all you want to do locally is ssh to the remote host, you could even [run ssh directly in the terminal](http://stackoverflow.com/a/10536794/324105), instead of a shell. In some cases you'd also want to make [eterm-color](http://stackoverflow.com/questions/22083177/eterm-color-unknown-terminal-type) a recognised terminal type on the remote host. – phils Sep 10 '14 at 22:29
  • Following all these steps worked but the connection is super laggy used from org-mode (compared to launching the R session via org-mode/ess)? Any suggestions? – story Jul 06 '22 at 10:09