Some simple functions for ESS and Sweave

david.whiting at david.whiting at
Wed Sep 1 14:55:52 CEST 2004


I have written some very simple elisp functions that I have found useful 
and thought others might like to see. I dabble with elisp occasionally at 
best so there are probably better ways to do what I have done, but so far 
this seems to work for me. There are several things that are hard-coded, I 
use Linux and I think that this would probably not work in Windows (not as 
it is now anyway).

With these functions and key bindings all I need to do is open a .Rnw file 
and, assuming R is running, I press:

M-n s to Sweave the file, then
M-n l to run latex on the results of Sweave, then
M-n p to make and display a postscript file , or
M-n P to make and display a PDF version.


;;; Some quick utils to make working with Sweave() even more
;;; delightful than it already is.
;;; David Whiting.
;;; 2004-04-15.

;;; TODO:
;;; I want to be able to send ess-makeLatex a parameter to tell it the 
number of
;;; times to run LaTeX (to get references updated correctly).
;;; Also need to add ess-makeBibtex.

;;; Might be good to have a way to chain commands.

(defun ess-makeSweave ()
   "Run Sweave on the current .Rnw file."
     ;; Make sure tools is loaded.
     (setq ess-command (format "library(tools)"))
     (ess-execute ess-command)
     (message "Sweaving %S" (buffer-file-name))
     (setq ess-command (format "Sweave(%S)" (buffer-file-name)))
     (ess-execute ess-command 'buffer nil nil)))

(defun ess-makeLatex ()
   "Run LaTeX on the product of Sweave()ing the current file."
     (setq thisbuffer (buffer-name))
     (setq namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
     (setq latex-filename (concat namestem ".tex"))
     (message "Running LaTeX ..." )
     (switch-to-buffer "*tex-output*")
     (call-process "latex" nil "*tex-output*" 1 latex-filename)
     (switch-to-buffer thisbuffer)
     (message "Finished running LaTeX" )))

(defun ess-makePS ()
   "Create a postscript file from a dvi file (name based on the current 
Sweave file buffer name) and display it with gv."
   (setq namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
   (setq dvi-filename (concat namestem ".dvi"))
   (setq my-command-string (concat "dvips -o " dvi-filename))
   (shell-command my-command-string)
   (shell-command "gv & "))

(defun ess-makePDF ()
   "Create a PDF file and display it with acroread."
   (setq namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
   (setq tex-filename (concat namestem ".tex"))
   (setq my-command-string (concat "pdflatex " tex-filename))
   (shell-command my-command-string)
   (setq my-command-string (concat "acroread " namestem ".pdf &"))
   (shell-command my-command-string))

;;; Now bind some keys.
(define-key noweb-minor-mode-map "\M-ns" 'ess-makeSweave)
(define-key noweb-minor-mode-map "\M-nl" 'ess-makeLatex)
(define-key noweb-minor-mode-map "\M-np" 'ess-makePS)
(define-key noweb-minor-mode-map "\M-nP" 'ess-makePDF)

More information about the ESS-help mailing list