New R Package: rmote
I often find myself in the situation where I need to log into a remote machine to do analysis in R but do not have a satisfactory way to view output of graphics being generated in the remote session. There are many possible ways to work graphically with R on a remote machine, but some are not very desirable and others are often not possible. In this post I introduce a new package “rmote” that provides a way to deal with this situation.
The “rmote” package provides a way to serve graphics from R running on a remote machine back to a web browser running on a local machine. It has the following features:
- Serves help, traditional (lattice, ggplot2) graphics, and htmlwidgets from a remote R session to a local web browser, using the servr package
- History of outputs is preserved and can be navigated (making it useful to run locally as well)
- Output in the web browser is refreshed live as commands are executed (again thanks to servr)
- Only requires a terminal and a web browser
- Does not use X11
Before getting into some details about the motivation, etc., here is a screen capture showing how it works:
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 or -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.
- 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 user@remote
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"))
Now run the following in R on the remote:
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 machine will automatically update to show the results. For example, try running each of the following in succession on the server:
?lm library(ggplot2) qplot(mpg, wt, data = mtcars, colour = cyl) library(lattice) dotplot(variety ~ yield | year * site, data=barley) library(rbokeh) figure() %>% ly_hexbin(rnorm(10000), rnorm(10000))
You can stop the rmote server with
rmote::stop_rmote(). This will turn the server off and cause all print methods to return to their default behavior.
Note that once you have an rmote server running in one R session on your remote machine, you can tap into that server in other remote R sessions by using the
rmote::rmote_on() function, providing the server directory from which the rmote server is running.
Also note that you can set several graphics device options to control traditional plot dimensions and resolution using
A final thought: rmote will have a hard time making it onto CRAN since it overwrites some standard R S3 print methods. If anyone has any clever ideas for overriding R print methods in a less-intrusive way but that is still convenient, let me know.