|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- tutorials |
| 4 | +date: "2021-01-06" |
| 5 | +tags: |
| 6 | +- emacs |
| 7 | +- programming |
| 8 | +- bioinformatics |
| 9 | +- R |
| 10 | +- remote |
| 11 | +title: "Getting Started with R Programming in Emacs: working on a remote server" |
| 12 | +slug: emacs-tutorial-remote |
| 13 | +author: "Chris Wallace" |
| 14 | +--- |
| 15 | + |
| 16 | +It can be slow and frustrating to run R via a GUI when your data and R are on a remote server. Emacs has long had support for editing remote files via [tramp](https://www.gnu.org/software/tramp/), and ESS allows emacs to interact with a remote R process. |
| 17 | + |
| 18 | +In this doc, I assume |
| 19 | +- emacs is installed on the local machine `A` |
| 20 | +- R is installed on the remote server `B` |
| 21 | +- you know how to ssh from `A` to `B` |
| 22 | +- you have installed ESS as described in [intro to ESS](https://github.com/ess-intro/presentation-ess-base-install) |
| 23 | +Your R script files can be on either `A` or `B`. |
| 24 | + |
| 25 | +# Starting a remote R process within emacs |
| 26 | + |
| 27 | +First open a file or directory on the remote server over ssh. |
| 28 | + |
| 29 | +`C-x C-f /ssh:user@B:~/ RET` |
| 30 | + |
| 31 | +If you have aliases set up in your `$HOME/.ssh/config` you can use the alias here instead of `user@B`. Then if you start an R process from that buffer, it will call R on the machine `B`. Do this by |
| 32 | + |
| 33 | +`M-x R` |
| 34 | + |
| 35 | +That's it, you have a running R process on the remote machine. Type |
| 36 | + |
| 37 | +`system("hostname")` |
| 38 | + |
| 39 | +to check. |
| 40 | + |
| 41 | +# Interacting with a remote R process from an R script on a remote server |
| 42 | + |
| 43 | +Open the remote R script over ssh. E.g. if the script is called myscript.R in your remote home directory, do |
| 44 | + |
| 45 | +`C-x C-f /ssh:user@B:~/myscript.R RET` |
| 46 | + |
| 47 | +Start an R process from that buffer, it will split the window if needed, and start on the machine `B`. Do this by |
| 48 | + |
| 49 | +`M-x R` |
| 50 | + |
| 51 | +That's it, you should have a running R process on the remote machine. All the standard ESS commands should work. So if you type a line in the script file, you can run it in the R process using |
| 52 | + |
| 53 | +`C-c RET` |
| 54 | + |
| 55 | +or run a block of code using |
| 56 | + |
| 57 | +`C-c C-c` |
| 58 | + |
| 59 | +See the [intro to ESS](https://github.com/ess-intro/presentation-ess-base-install) to learn more. |
| 60 | + |
| 61 | +# Configuring tramp |
| 62 | + |
| 63 | +While tramp comes with emacs, there are a couple of settings that can help if you add them to your emacs config file |
| 64 | + |
| 65 | +``` |
| 66 | +;; ssh seems the most reliable connection method |
| 67 | +(setq tramp-default-method "ssh") |
| 68 | +;; this sets the path on the remote server according to the ~/.profile file on the remote machine. Useful if your R is in a non-standard location |
| 69 | +(add-to-list 'tramp-remote-path 'tramp-own-remote-path)) |
| 70 | +``` |
0 commit comments