[ESS] ESS/R transcript with embedded graphics

Vitalie S. vitosmail at rambler.ru
Wed Aug 5 21:12:22 CEST 2009


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 in  
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 to  
get `allout` headers converted to HTML headers (i.e. ###_ to H1, ###_* to  
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 output  
to png device and automatically display newly created images by use of  
plot.new hook in R. In this way avoiding placing additional showplot after  
each plot().

It work as folows:

#start a Report session with
StartReportRec()
#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(sin)
plot(cos); plot(acos)
plot(tan)
par(tpar)

StopReportRec()

The output would look like:

> StartReportRec()
Report session started with device:
png:c:/spinus/works/img/pic_05-08-09_20-52-13_%04d.png
                                                      4
> 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 two  
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
     count<-1
     outDir <- gsub("\\\\", "/", outDir)
     if(!file.exists(outDir)) dir.create(outDir)
     name <-  
file.path(outDir,paste(nameImg,"_",format(Sys.time(),format="%d-%m-%y_%H-%M-%S"),"_","%04d.png",sep=""))
     png(filename=name, height=height, width=width, ...)
     display.png<-function(){
         cat("   ******   {{",sprintf(name,count), "}}    ****** \n", sep =  
"")
         count<<-count+1
         invisible(NULL)
     }
     setHook('plot.new', NULL, 'replace')
     setHook('plot.new',display.png)
     assign(".IIMAGE.DEVICE", dev.cur(), envir=globalenv())
     cat("Report session started with device:\n")
     print(dev.cur())
     invisible(NULL)
}

StopReportRec <- function(){
     dev.name <- get(".IIMAGE.DEVICE", envir=globalenv())
     if(!is.null(dev.name)){
         setHook('plot.new', NULL, 'replace')
         dev.off(dev.name)
         assign(".IIMAGE.DEVICE", NULL, envir=globalenv())
         cat("Report session stopped at", as.character(Sys.time()), " and  
`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 :(.

Best,
Vitalie.









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 iimage.el
> doesn't like back slashes
>     din <- dev.size("in")
>     dev2bitmap(file, width = din[1], height = din[2], taa = 4, gaa = 2,
> ...)
>     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 t)
>       (replace-match (concat "<img src='\\1'/>")))
>     ;; cludge for Firefox/windows absolute paths: add // in front of c:
>     (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)


--



More information about the ESS-help mailing list