R users often find themselves needing to log in to a remote machine to do analysis. Sometimes this is due to data restrictions, computing power on the remote machine, etc. Users can ssh in and run R in a terminal, but it is not possible to look at graphics, etc.
There are currently three approaches that I am aware of to deal with this:
- Install RStudio Server on the remote server and use that from a web browser on your local machine. Graphics output is shown in the IDE.
- Use X11 forwarding (
ssh -X|Y
). Graphics output is sent back to your machine. - Use VNC with a linux desktop environment like KDE or GNOME.
Whenever possible, #1 is by far the best way to go and is one of the beautiful things about RStudio Server. #2 is not a good choice - plots are very slow to render, the quality is terrible, and it doesn't support recent advances in plot outputs like htmlwidgets (unless you launch firefox through X11, which will mean you might get to look at one plot per hour). #3 is okay if it is available and you are comfortable working in one of these desktop environments.
There could be other obvious ways to deal with this that I am oblivious to.
Often we do not have the choice of installing RStudio Server or a desktop environment on the remote machine. Also, some users prefer to work in a terminal sending commands from a favorite text editor on a local machine, but still want to see graphics. We would like to have something better than X11 forwarding to view graphics and other output when running R in a terminal on a remote machine.
The rmote package is an attempt to make working in R over ssh on a server a bit more pleasant in terms of viewing output. It uses servr on the remote machine to serve R graphics as they are created. These can be viewed on the local machine in a web browser. The user's local browser will automatically refresh each time a new output is available.
Currently there is support for lattice, ggplot2, htmlwidgets, and help output.
-
Choose a port to run your remote server on (default is 4321)
-
ssh into the remote machine, mapping the port on the remote back to your local machine:
ssh -L 4321:localhost:4321 -L 8100:localhost:8100 user@remote
I also add port 8100 so I can forward shiny apps back to my local machine on a dedicated port.
-
On the remote machine launch R and install the latest version of servr and rmote (one time only)
install.packages("rmote", repos = c(CRAN = "http://cran.rstudio.com", tessera = "http://packages.tessera.io"))
or alternatively
devtools::install_github(c("yihui/servr", "hafen/rmote"))
Note that this package will probably never be on CRAN since it overwrites some standard R S3 methods.
-
Run the following in R on the remote:
rmote::start_rmote()
To view some of the options for this, see
?start_rmote
. One option is the port, which needs to match the one your forwarded in step 2 (4321 is the default.) -
On your local machine, open up your web browser to
localhost:4321
Now as you create compatible plots on your remote machine, your browser on your local fmachine will automatically update to show the results. For example, try running each of the following in succession on the server:
?plot qplot(mpg, wt, data=mtcars, colour=cyl) dotplot(variety ~ yield | year * site, data=barley) library(rbokeh) figure() %>% ly_hexbin(rnorm(10000), rnorm(10000))
This process is slightly more tedious than ssh -X
for initial setup, but much faster and more functional.
Another interesting package to check out that allows you to control R on a remote machine from your local R session is - remoter.
If you must work on a remote terminal, here are some additional utilities that help make things nice:
This package is not yet on CRAN. To install the latest development version you can install from the cloudyr drat repository:
# latest stable version
install.packages("rmote", repos = c(getOption("repos"), "http://cloudyr.github.io/drat"))
Or, to pull a potentially unstable version directly from GitHub:
if(!require("ghit")){
install.packages("ghit")
}
ghit::install_github("cloudyr/rmote")