[ESS] ESS/R transcript with embedded graphics

Short, Tom TShort at epri.com
Wed Aug 5 22:13:43 CEST 2009

I think this fixes the regex, so multiple graphics on a line work:

(setq iimage-mode-image-regex-alist
         `((,(concat "\\({{\\)"
                "\\(}}\\)") . 2))) 

- Tom

-----Original Message-----
From: ess-help-bounces at stat.math.ethz.ch
[mailto:ess-help-bounces at stat.math.ethz.ch] On Behalf Of Short, Tom
Sent: Wednesday, August 05, 2009 3:49 PM
To: Vitalie S.; ess-help at stat.math.ethz.ch
Subject: Re: [ESS] ESS/R transcript with embedded graphics

That's a good way to do it, Vitalie.  One thing you might want to do is
add a hook for grid graphics (lattice, ggplot, etc) as:

 setHook('plot.new', display.png)
 setHook("grid.newpage", display.png)

{I haven't tried it yet.}

As far as multiple graphics on one line, it's probably in the regex that
I defined, but I'm not good enough with them to debug it properly. If
you use the original regex list, it will show more than one on a line
(but it tries to turn too many things into graphics).

(setq iimage-mode-image-regex-alist
  `((,(concat "\\({{\\)"
          "\\(}}\\)") . 2)))

- Tom

-----Original Message-----
From: Vitalie S. [mailto:vitosmail at rambler.ru] 
Sent: Wednesday, August 05, 2009 3:12 PM
To: ess-help at stat.math.ethz.ch; Short, Tom
Subject: RE: [ESS] ESS/R transcript with embedded graphics

Hi Tom,

Thanks for the code. It's indeed a quick and pretty nice way of making  
reports on the fly.

The iimage mode could potential serve for a notebook type of interface
emacs (adding to current functionality of comint an option to insert the

output in place would do the job, I believe).

Another great improvement (as I see it) would be modifying htmlize code
get `allout` headers converted to HTML headers (i.e. ###_ to H1, ###_*
H2 etc). And may be convert self documenting comments to some useful  
format (Roxigen type of thing).

For the time being, I wrote two functions for diverting the device
to png device and automatically display newly created images by use of  
plot.new hook in R. In this way avoiding placing additional showplot
each plot().

It work as folows:

#start a Report session with
#Start executing your session.
#from now on all images are diverted in %curentdir%/img/ directory

tpar <- par(cex=.8, mar=c(3.5, 3.5, 2, 1))
plot(cos); plot(acos)


The output would look like:

> StartReportRec()
Report session started with device:
> tpar <- par(cex=.8, mar=c(3.5, 3.5, 2, 1))
> plot(sin)
    ******   {{c:/spinus/works/img/pic_05-08-09_20-52-13_0001.png}}     
> plot(cos); plot(acos)
    ******   {{c:/spinus/works/img/pic_05-08-09_20-52-13_0002.png}}     
    ******   {{c:/spinus/works/img/pic_05-08-09_20-52-13_0003.png}}     
> plot(tan)
    ******   {{c:/spinus/works/img/pic_05-08-09_20-52-13_0004.png}}     
> par(tpar)
> StopReportRec()
Report session stopped at 2009-08-05 20:52:15  and `png` device closed.

Now by executing C-l (iimage-recenter) recorded pics will display.

There is a glitch though in iimage.el. I couldn't make work to display
or more images on the same line:
{{pic_1.png}} {{pic_2.png}} {{pic_3.png}}
Any ideas?


StartReportRec <- function(outDir=file.path(getwd(), "img"),width=180,  
height=width, nameImg="pic", ...){
# outDir - output directory
# width - images width
# nameImg - name to be prefixed to the image name (constructed with  
Sys.time below)
# ... other parameters to be passed over to png device
     outDir <- gsub("\\\\", "/", outDir)
     if(!file.exists(outDir)) dir.create(outDir)
     name <-  
     png(filename=name, height=height, width=width, ...)
         cat("   ******   {{",sprintf(name,count), "}}    ****** \n",
sep =  
     setHook('plot.new', NULL, 'replace')
     assign(".IIMAGE.DEVICE", dev.cur(), envir=globalenv())
     cat("Report session started with device:\n")

StopReportRec <- function(){
     dev.name <- get(".IIMAGE.DEVICE", envir=globalenv())
         setHook('plot.new', NULL, 'replace')
         assign(".IIMAGE.DEVICE", NULL, envir=globalenv())
         cat("Report session stopped at", as.character(Sys.time()), "
`png` device closed.\n")

I belive the same thing could be achieved by monitoring for changes the

images directory from inside the emacs directly but that is too  
complicated for me :(.


On Wed, 05 Aug 2009 04:43:10 +0200, Short, Tom <TShort at epri.com> wrote:

> This email describes two things I've cobbled together:
>   + HTML export of an R session with syntax highlighting and inline
>     graphics (prettyR is a different way to get this). See enclosed
>     for a pdf of a part of a transcript of the graphics demo printed
>     to pdf.
>   + Transcript of an R session with inline graphics in Emacs
> It's a way to document work during the "exploratory stage".
> Both are independent of each other. Both require you to use the
> function "showplot" to create a copy of the graphic and indicate where
> to put it. So you need to do:
>> plot(sin)
>> showplot() # the image is shown after this
>> plot(cos)
>> showplot("cos.png") # give it a local name
> First, here's showplot:
> showplot <- function(file = paste(tempfile(), ".png", sep = ""), ...)
>     file <- gsub("\\\\", "/", file)    # Needed on windows as
> doesn't like back slashes
>     din <- dev.size("in")
>     dev2bitmap(file, width = din[1], height = din[2], taa = 4, gaa =
> ...)
>     cat("{{", file, "}}\n", sep = "")
> }
> This copies the current graph to a PNG file (a temporary file by
> default). It outputs the name of the buffer between a set of curly
> brackets. These curly brackets are used to insert the graphic into the
> emacs buffer and for exporting to HTML.
> HTML export
> ------------
> This relies on htmlize by Hrvoje Niksic.
> http://www.emacswiki.org/emacs/Htmlize
> Run the following emacs lisp function to htmlize and launch your
> browser. In the process, it substitutes the {{whatever}} with
> appropriate html links.
> (defun htmlize-buffer-with-images ()
>   "Convert buffer to html, including embedded images.
> Creates a file called emacs-html-export.html in the current directory"
>   (interactive)
>   (require 'htmlize)
>   (save-excursion
>     (switch-to-buffer (htmlize-buffer (current-buffer)))
>     (goto-char (point-min))
>     ; replace the {{file.png}} with <img src='file.png'/>
>     (while (re-search-forward "{{\\([-+./_0-9a-zA-Z:\\\\].*\\)}}" nil
>       (replace-match (concat "<img src='\\1'/>")))
>     ;; cludge for Firefox/windows absolute paths: add // in front of
>     (goto-char (point-min))
>     (while (re-search-forward "<img src='\\(.:\\)" nil t)
>       (replace-match (concat "<img src='//\\1")))
>     (write-file "emacs-html-export.html")
>     (browse-url-of-file)))
> Inline images in your emacs buffer
> -----------------------------------
> This uses iimage, a minor mode by KOSEKI Yoshinori. It's included with
> GNU Emacs 23 and is also here:
> http://www.netlaputa.ne.jp/~kose/Emacs/iimage.html
> You need the following for your regex for image replacement, then just
> run "iimage-mode".
> (setq iimage-mode-image-regex-alist
>   `((,(concat "\\({{\\)"
>           "\\([:-+./_0-9a-zA-Z].*\\)"
>           "\\(}}\\)") . 2)))
> With iimage, it doesn't automatically show graphics. Maybe a
> hook could be added.
> Note that most of this is only useful with ess-eval-visibly-p set
> to t. Finally, this has only been tested on GNU Emacs 22 under
> Windows XP with R 2.9.0.
> - Tom
> Tom Short
> Electric Power Research Institute (EPRI)

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

ESS-help at stat.math.ethz.ch mailing list

More information about the ESS-help mailing list