Feature request: Send plots from R in Terminal to Plots pane

terminal

#1

One useful workflow enabled by the new support for the Terminal in v1.1 is to SSH to a remote compute cluster directly from the RStudio Terminal. This allows you to have the nice RStudio development environment, and then send your code to the Terminal with Ctrl+Alt+Enter to use the computational resources of the remote machine and also have direct access to the files on the remote machine. One downside of this approach is that plots created in the remote R session running in the RStudio Terminal are displayed in an X11 window. Would it be possible to send these plots to the RStudio plots pane?

I searched various places for “terminal plot” to see if this had been discussed before, but I wasn’t able to find anything:


#2

Hi @jdblischak, is your current workaround to save the ggplot2 output as PNG and then download it? Another option could be to install RStudio Server directly in your server, this will allow you to use a web browser as your interface so the development takes place right inside the server. The RStudio Server Web UI looks just like the local IDE looks: https://www.rstudio.com/products/rstudio/download-server/


#3

Hey @jdblischak, one solution I use a lot is rmote. It doesn’t forward plots to the RStudio plot pane, but it does forward them to a server that you can access in a web browser. Basically, you install the package, SSH in with the port the server will listen on forwarded (which you specify in the SSH command) and then run the server in the remote R session (which you can do in your remote .Rprofile if you like). Then you just leave a browser open on localhost:XXXX (your forwarded port) and plots (ggplot, lattice or base), htmlwidgets and documentation pages open in the browser. You can choose which types of things go through it, and there’s a nice history pane in there too.

It’s not quite a native RStudio experience done remotely, but it beats the hell out of an X11 window :slight_smile: It’s also particularly nice because the servers I use are organisational—I can’t install RStudio Server on them, but I can install R packages :slight_smile:


#4

I’ve grown fond of imgcat in iTerm2, and discovered you can redefine a plot’s print method to pass the image to it, so it prints directly in the terminal:

Sadly though, imgcat is limited to iTerm2, so it doesn’t work in Terminal.app or RStudio’s terminal. A similar approach may let you define a new image device, though.


#5

Thanks, everyone, for the great suggestions!


#6

is your current workaround to save the ggplot2 output as PNG and then download it?

@edgararuiz Saving it and downloading it is not necessary. The plot pops up in an X11 window for immediate viewing. My issue is that it would be better if the plot was sent to the RStudio Plots pane. Then I wouldn’t have to leave RStudio to see my plot (and I could scroll through past iterations of the plot).

Another option could be to install RStudio Server directly in your server, this will allow you to use a web browser as your interface so the development takes place right inside the server.

I agree 100% that the ideal solution is to use RStudio Server. However I am exploring workarounds because of my current computing situation. My university’s HPC won’t host RStudio Server for all users because it doesn’t do load balancing, and they are not yet willing to pay for the Pro version that includes this feature (I’m doing my best to convince them otherwise). Thus the only option available for my lab is to purchase a dedicated compute node to host our own private RStudio Server running on the university HPC. This is likely what we end up doing, but since it is expensive, I wanted to investigate the option of using SSH from the Terminal in RStudio.


#7

one solution I use a lot is rmote. It doesn’t forward plots to the RStudio plot pane, but it does forward them to a server that you can access in a web browser.

@rensa Very cool! I’ll need to check this out. I agree it’s way better than an X11 window, but doesn’t solve my central problem of not leaving RStudio. Also, I help many of my colleagues with their computational setup, so the fewer setup steps involved the better.


#8

I’ve grown fond of imgcat in iTerm2, and discovered you can redefine a plot’s print method to pass the image to it, so it prints directly in the terminal

@alistaire Very cool hack! Do you have detailed setup instructions posted somewhere so that others could set this up? I use Ubuntu, but many of my colleagues use macOS and may be interested in trying this out.


#9

Here’s the imgcat docs, which links to the actual imgcat script, but if you install iTerm2 (and maybe its shell integration; Menu > iTerm2 > Install Shell Ingegration) it should be at ~/.iterm2/imgcat, so the function from above should do the trick (for ggplot; for other graphics set up a similar print method):

print.ggplot <- function(x, width = 6, height = 4, dpi = 100, ...){
    path <- tempfile(fileext = '.png')
    ggplot2::ggsave(filename = path, plot = x, 
                    width = width, height = height, dpi = dpi, ...)
    system(paste('~/.iterm2/imgcat', path))
    file.remove(path)
}

You could put that in your .Rprofile, but I wouldn’t recommend it if you plan to use R with any other graphics device. An alternative is to define an explicit printing function, e.g.

imgcat <- function(x, ...){
    path <- tempfile(fileext = '.png')
    png(path, ...)
    x
    dev.off()
    system(paste('~/.iterm2/imgcat', path))
    file.remove(path)
}

which you can pass an expression to print, e.g.

imgcat(plot(1))

or

imgcat({
    palette(rainbow(2, s = .5, v = 0.6))
    stars(cbind(1:16, 10 * (16:1)), draw.segments = TRUE)
}, height = 300)

(h/t Yihui)

so


#10

Thank you @rensa for mentioning rmote! I was able to get it to work with the Sun Grid Engine cluster I have access to with the setup I describe at https://github.com/cloudyr/rmote/issues/7#issuecomment-366386514. I’m curious if you would do something in a different way from what I describe there.

This solution will work =) At least until RStudio adds the feature @ jdblischak requested ^^