Nvim-R icon indicating copy to clipboard operation
Nvim-R copied to clipboard

Feature request: Add iTerm2 in-line image support.

Open zhmiao opened this issue 9 years ago • 20 comments
trafficstars

Since new iTerm2 supports in-line images, I am wondering whether it is possible to output R graphs directly into iTerm so I don't have to use X11 anymore. iTerm image viewer is imgcat and there is an external matplotlib backend for iterm too. In addition, iTerm can also view pdfs (termpdf), although it is really beta for now.

zhmiao avatar Mar 28 '16 19:03 zhmiao

If someone writes the R code necessary for this, I can integrate it into nvimcom and, if necessary, add a key binding to Nvim-R to call it. Does the image display in a Tmux pane? Does it display in Neovim terminal inside iTerm2?

jalvesaq avatar Mar 28 '16 21:03 jalvesaq

It's also possible to display images in mlterm on Linux using img2sixel, but it does not work inside of either Neovim terminal or Tmux.

jalvesaq avatar Mar 29 '16 20:03 jalvesaq

Yes, neovim in iTerm2 don't show images either. This is the related issue https://github.com/neovim/neovim/issues/4349 it is still open. It seems w3m in terminal mode in neovim can show images, but I don't think it is useful for graphs in Nvim-R

zhmiao avatar Mar 29 '16 20:03 zhmiao

w3m will be useful if we display the image in an html file:

<html><body><img src="image.png"></body></html>

jalvesaq avatar Mar 29 '16 20:03 jalvesaq

Yeah! if we can view graphs within neovim, that would be so cool

zhmiao avatar Mar 29 '16 21:03 zhmiao

What I don't know is how to make all plot commands to output a png file. If plot(0) made a png, Nvim-R could start a new terminal, create the html to display the image and call w3m. Unfortunately, it would not work if inside Tmux.

jalvesaq avatar Mar 29 '16 21:03 jalvesaq

is it possible to integrate :terminal command?

zhmiao avatar Mar 29 '16 21:03 zhmiao

Yes, Nvim-R already uses termopen() (which is the same :terminal called in a different way) to start R.

jalvesaq avatar Mar 29 '16 21:03 jalvesaq

right, I forgot, it seems to output graphs to neovim terminal need some modifications in R itself

zhmiao avatar Mar 29 '16 21:03 zhmiao

R has many graphics devices (png, jpg, svg, X11, etc...). I think what we need is a new graphics device that do two things:

  1. Create a PNG file.
  2. Send a message to Nvim-R about the newly created file.

Nvim-R would react to the message and display the PNG. If there was a image already being displayed, w3m would first be closed and then called again.

jalvesaq avatar Mar 29 '16 21:03 jalvesaq

Are you familiar with how R markdown works? I am not sure how they show pictures in these markdown files

zhmiao avatar Mar 29 '16 21:03 zhmiao

I don't use markdown. Do you have an link to an example?

jalvesaq avatar Mar 29 '16 21:03 jalvesaq

Maybe you can check this link R_Markdown And this is their repo

zhmiao avatar Mar 29 '16 21:03 zhmiao

The links are for RStudio, not for Vim. RStudio is a Qt application and it has its own graphics device. Because Qt is a GUI toolkit, it's easy to integrate graphical things in it.

jalvesaq avatar Mar 29 '16 22:03 jalvesaq

I see. Is making a new graphic device complicated?

zhmiao avatar Mar 29 '16 22:03 zhmiao

I don't know to make one, and I don't have time to look at this. If you can create the new graphic device, I can integrate it into Neovim. However, there is another problem: the size of the window displaying the image would be difficult to control. See: https://github.com/jalvesaq/Nvim-R/issues/33

jalvesaq avatar Mar 29 '16 22:03 jalvesaq

yes, let me look at this.

zhmiao avatar Mar 29 '16 22:03 zhmiao

I have had a look at this. Creating a new graphics device seems hard. Knitr uses the evaluate package to watch for changes in the graphics device.

Relevant files:

  • https://github.com/yihui/knitr/blob/c3085229f5a02506e40f21c1cecf6d8448f5caaa/inst/examples/knitr-graphics.lyx "There are two sources of plots ..."
  • https://github.com/yihui/knitr/blob/c3085229f5a02506e40f21c1cecf6d8448f5caaa/R/plot.R "save a recorded plot"
  • https://github.com/hadley/evaluate/blob/cc0c5847a9990ae91467a24a671c8688bc5b84ec/R/watcher.r which calls "watch_snapshot"
  • https://github.com/hadley/evaluate/blob/cc0c5847a9990ae91467a24a671c8688bc5b84ec/R/graphics.r where "watch_snapshot" is defined, with some helpful comments.

Relevant commands:

  • dev.control()
  • recordPlot()

Someone has been thinking about a better way to do this from within R, but has made no progress for a while. https://github.com/pmur002/R-display-list/blob/master/capture.plots.R

The markdown package is R, not RStudio, though it was developed by the RStudio company.

nacnudus avatar Apr 08 '16 18:04 nacnudus

I think there is a major issue in implementing a new graphic device for imagecat. In R, a graphic is not ready to save/print until dev.off() is invoked. For knitr/rmarkdown, it is easier for them because they know when a plot is ready.

randy3k avatar Jun 23 '16 02:06 randy3k

w3m 0.5.3+git20151119 can display images in tmux 2.1. So, if this feature was implemented it could work in both Mac OS X and Linux.

jalvesaq avatar Jul 04 '16 09:07 jalvesaq