[ESS] ESS/R transcript with embedded graphics

Vitalie S. vitosmail at rambler.ru
Thu Aug 6 18:52:10 CEST 2009


Hi,

I modified original set of regexps.

(autoload 'iimage-mode "iimage" "Support Inline image minor mode." t)
(autoload 'turn-on-iimage-mode "iimage" "Turn on Inline image minor mode."  
t)

(setq iimage-mode-image-filename-regex
	(concat "[-+./_0-9a-zA-Z:]+\\."
		(regexp-opt (nconc (mapcar #'upcase
					   image-file-name-extensions)
				   image-file-name-extensions)
			    t)))

;(add-hook 'ess-mode-hook 'ess-iimage-options)
(add-hook 'inferior-ess-mode-hook 'ess-iimage-options)

(defun ess-iimage-options()
   (set (make-local-variable 'iimage-mode-image-regex-alist)
        	`((,(concat "\\({{\\)"
		    "\\(" iimage-mode-image-filename-regex "\\)"
		    "\\(}}\\)") . 2))
))


Also modified your original htmlize function by adding <center> tag for  
images and hiding htmlize buffer at the end:

(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 "<center><img  
src='\\1'/></center>")));<center><img src=http://YOUR PICTURE URL></center>
     ;; 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  (concat "Report_" (replace-regexp-in-string "\\W" ""  
(buffer-name (other-buffer))) ".html"))
     (browse-url-of-file)
     (switch-to-buffer (other-buffer))
))

The whole story can be embedded in a emacs interactive command; say  
selected region is evaluated into newly created buffer, activate iimage,  
and then htmlize. I am a bit cut in time but may be will give it a thought  
in a couple of months.

I experience problems evaluating regions with more than 6 lines with  
`ess-eval-visibly-p true under Windows. Emacs just hangs and interrupts  
evaluation when C-g pressed. Somebody mentioned this long long back,  
apparently the issue is still unsolved. Does anybody have the same problem?

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

>  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)
>


Yes, sure, here is a new code with additional endLine parameter added.  
Sometimes is  useful to display two or more images in a row:  
plot(sin);plot(cos). I have no idea how to detect this and just using  
endLine="" for now:


StartReportRec <- function(outDir=file.path(getwd(), "img"),width=250,  
height=width, nameImg="pic", ..., endLine="\n"){
     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), "}} ", endLine, sep = "")
         count<<-count+1
         invisible(NULL)
     }
     setHook('plot.new', display.png)
     setHook("grid.newpage", display.png)
     assign(".IIMAGE.DEVICE", dev.cur(), envir=globalenv())
     cat("Notebook session started with the device ")
     print(dev.cur())
     invisible(NULL)
}

Vitalie.



More information about the ESS-help mailing list