A Handy Trick for Remote Graphics
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I often create plots that require quite a bit of computation. Ideally I would run this on what I’ll call Machine A, which is a very fast machine, but I am often far away, on Machine B. So, I’d like to run my computation on B but display it on A.
For the platforms I use (Linux, Mac), I can simply use X11 forwarding, by typing ssh -Y at B to log into A. You may not have that capability, so I’ll give you a simple method here to accomplish the same thing without X11. And as a bonus, if you’ve never used R sockets, you can learn something about them along the way. (You will need the ability to run a server at B. But just try the commands below to check whether you can do this.) Here’s what to do:
1. In an R session at A, run
> con <- socketConnection(port=11000,server=T,
blocking=T,open="a+b",timeout=600)
This starts the server, but the above command will hang until we run a corresponding command at B:
> con <- socketConnection(host=quoted_address_of_A,port=11000,
server=F,blocking=T,open="a+b",timeout=600)
At this point you will have your R prompts at both A and B, so you’re all set.
Now run your computation at A. For instance, I might use my freqparcoord package (see several of my earlier blog postings), which returns the graph in a ggplot2 object. I feed that object into R’s serialize() function, which encodes it and then sends it through my socket con:
> serialize(freqparcoord(tx50,10,c(6,9,11:14)),con)
Then at B, I receive it:
> unserialize(con)
That reads the message from con, decodes it, and prints the result. The latter action results in the screen display.
You can keep doing this as long as you like. Note that I did set a 10-minute timeout period, so if you will have long thoughts, frequent phone interruptions etc., you may wish to set a longer period. You can close the connection by typing
> close(con)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.