[ESS] Displaying plots in Emacs

Titus von der Malsburg malsburg at posteo.de
Fri May 15 21:30:52 CEST 2015

One feature that I really miss when developing R code with ESS is the
ability to display plots in Emacs.  The free-floating plots generated
R’s plotting functions are really annoying because they pop up in random
positions and almost always cover stuff that I need to see.

Last time I checked, people said that the only way to have plots in
Emacs windows would be use the XWidget branch of Emacs which allows
embedding of GTK widgets into Emacs buffers.  This may be a neat
solution (although R doesn’t seem to use GTK) but it’s not going to
happen anytime soon because the XWidget branch is pretty dead (last
commit in Sept 2013).

Luckily, there may be a much simpler solution: Emacs has a fantastic
support for viewing PDF files via the pdf-tools package [1].  (For
people who don’t know pdf-tools, it allows you to isearch PDF files and
to create and edit annotations that a compatible with those created by
Adobe tools.  Amazing!)  So one solution is to plot into a PDF file and
to display that PDF in a separate Emacs buffer window.

This has the potential to be really convenient.  For example, we can
activate auto-revert-mode in the buffer displaying the PDF such that the
PDF is automatically refreshed when a new plot is written to the PDF
file.  Also pdf-tools can scale the plot to fill the window.  This means
that we can resize the window and the size of the plot is dynamically
adapted – no need to replot.

So far, this solution works really nicely and certainly much better than
the x11 windows created by default.  However, there are two things that
are suboptimal:

1.) It is annoying that I have to type and execute:


instead of just:


Is there a way to use the PDF renderer as the default device instead of
x11?  If not (which is likely), what else can be done to free the user
>From having to generate the PDFs manually?  Is there perhaps a clever
way to wrap the plotting functions?  Something involving recordPlot and

2.) It would be nice if plotting would automatically open a new Emacs
buffer displaying the result.  This could perhaps be addressed by
scanning each executed command with a regular expression, something like
(but realistically much more complex than):

  (defvar ess-plot-command-regexp "\\bplot(")

If it matches, ESS could open the PDF in a new buffer window or prompt
the user asking whether the PDF should be opened.  Is there already
infrastructure in ESS for scanning R commands like that?

An alternative might be to use gfilenotify/inotify/w32notify to inform
ESS every time the target PDF changes.  ESS could then prompt the user
asking whether the plot should be displayed.  This is cleaner because
detecting plotting commands accurately is at least difficult.  However
the notify solution requires that ESS knows the PDF containing the plot
but this could be achieved by whatever the solution for issue 1 is.

What are your thoughts on this?  Does the PDF approach sound like the
way to go?  Any ideas regarding the two issues above?  It would be great
to get some feedback on this.


[1] https://github.com/politza/pdf-tools

More information about the ESS-help mailing list