R tips and tricks

From SESLINK Wiki
Revision as of 15:35, 30 January 2017 by Ingo (talk | contribs) (Tips and Tricks around R)
Jump to navigation Jump to search

R tips and tricks

R in the clouds

Run a local script on a remote (fast) computer directly without RServer

Prerequsites:

  • you can access the remote computer with 'ssh' (secure shell) from a terminal
  • R (and necessary packages) is installed on the remote computer
  • R has access to all the data that are analyzed (upload i.e. with 'rsync', 'sftp' or filezilla)
  • to see plots directly you a local X-server installed:
    • MacOS: XQuarz,
    • Windows:
      MobaXterm - a powerful X-Server with many remote access features or
      Xming, a minimalistic X server for Windows
    • Linux: ... needs nothing ;-)

Procedure:

  1. Write your R script in a(ny) text editor (perferably one with R syntax highlight mode and bracket auto-close: e.g. Geany, Gedit, TextWrangler, Atom, etc.)
  2. Save it (Ctrl-s/Cmd-s). Do not close the editor!
  3. Open a terminal
  4. use commmand:

    ssh -X user@ssh-server 'R --slave' < ~/path-to/MyRscript.R

    (exchange 'user', 'ssh-server' and 'MyScript.R' accordingly; mind the ' ' in the command)
  5. Look at your results,
  6. modify your script, save ('Ctrl-s/Cmd-s')
  7. re-run terminal command (use the 'up arrow' on your keyboard to get it back)

Repeat steps 6 and 7 as often as needed

Comments:

  • The '--slave' flag tells R not to be interactive and show no terminal output. If you want output you need to store it locally with 'sink()' in your script
  • The '-X' flag in ssh allows you to have a result plot shown directly as a pop-up window.
  • The '~' is the sign for 'my Home directory'
  • In Windows exchange path separators '/' for '\'
  • Important: In Windows the X-Servers have to be running before conducting the R-script!
  • In MacOS XQuarz sometimes does not start automatically or it takes some time; start it manually if nothing shows up
  • In order to avoiding loggin in with password into the remote machine every time create a key pair with the commands:
    ssh-keygen (creates a private and a public key pair)
    ssh-copy-id user@ssh-server (exports the public key to the remote machine)