How to easily execute R commands on remote server?

Adam Ryczkowski picture Adam Ryczkowski · Feb 27, 2014 · Viewed 23.7k times · Source

I use Excel + R on Windows on a rather slow desktop. I have a full admin access to very fast Ubuntu-based server. I am wondering: how to remotely execute commands on the server?

What I can do is to save the needed variables with saveRDS, and load them on server with loadRDS, execute the commands on server, and then save the results and load them on Windows.

But it is all very interactive and manual, and can hardly be done on regular basis.

Is there any way to do the stuff directly from R, like

  1. Connect with the server via e.g. ssh,
  2. Transfer the needed objects (which can be specified manually)
  3. Execute given code on the server and wait for the result
  4. Get the result.

I could run the whole R remotely, but then it would spawn a network-related problems. Most R commands I do from within Excel are very fast and data-hungry. I just need to remotely execute some specific commands, not all of them.

Answer

Heisenberg picture Heisenberg · Nov 17, 2014

Here is my setup.

  1. Copy your code and data over using scp. (I used github, so I clone my code from github. This has the benefit of making sure that my work is reproducible)

  2. (optional) Use sshfs to mount the remote folder on your local machine. This allows you to edit the remote files using your local text editor instead of ssh command line.

  3. Put all things you want to run in an R script (on the remote server), then run it via ssh in R batch mode.