Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily execute R commands on remote server?

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.

like image 475
Adam Ryczkowski Avatar asked Feb 27 '14 09:02

Adam Ryczkowski


2 Answers

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.

like image 151
Heisenberg Avatar answered Oct 11 '22 14:10

Heisenberg


There are a few options, the simplest is to exchange secure keys to avoid entering SSH/SCP passwords manually all the time. After this, you can write a simple R script that will:

  1. Save necessary variables into a data file,
  2. Use scp to upload the data file to ubuntu server
  3. Use ssh to run remote script that will process the data (which you have just uploaded) and store the result in another data file
  4. Again, use scp command to transfer the results back to your workstation.

You can use R's system command to run scp and ssh with necessary options.

Another option is to set up cluster worker at the remote machine, then you can export the data using clusterExport and evaluate expressions using clusterEvalQ and clusterApply.

like image 28
df239 Avatar answered Oct 11 '22 12:10

df239