Editing/adding on to Sweave features in ESS

ESS
R
Author

Vinh Nguyen

Published

May 14, 2009

I really like David Whiting's Sweave methods in ESS: M-n s M-n P and I get a compiled pdf file. However, i wanted to change it so Sweave uses the cacheSweaveDriver() when sweaving. I knew the the functions were defined in the ess-swv.el file (in my carbon emacs in Mac OS X, it is at /Applications/Emacs.app/Contents/Resources/site-lisp/ess/ess-swv.el). Now, I edited the file to create a new way to Sweave:

;;; changed by vinh
(defun ess-swv-run-in-R2 (cmd &optional choose-process)
 "Run \\[cmd] on the current .Rnw file. Utility function not called by user."
 (let* ((rnw-buf (current-buffer)))
 (if choose-process ;; previous behavior
 (ess-force-buffer-current "R process to load into: ")
 ;; else
 (update-ess-process-name-list)
 (cond ((= 0 (length ess-process-name-list))
 (message "no ESS processes running; starting R")
 (sit-for 1); so the user notices before the next msgs/prompt
 (R)
 (set-buffer rnw-buf)
 )
 ((not (string= "R" (ess-make-buffer-current))); e.g. Splus, need R
 (ess-force-buffer-current "R process to load into: "))
 ))

 (save-excursion
 (ess-execute (format "require(tools)")) ;; Make sure tools is loaded.
 (basic-save-buffer); do not Sweave/Stangle old version of file !
 (let* ((sprocess (get-ess-process ess-current-process-name))
 (sbuffer (process-buffer sprocess))
 (rnw-file (buffer-file-name))
 (Rnw-dir (file-name-directory rnw-file))
 (Sw-cmd
 (format
 "local({..od <- getwd(); setwd(%S); %s(%S, cacheSweaveDriver()); setwd(..od) })"
 Rnw-dir cmd rnw-file))
 )
 (message "%s()ing %S" cmd rnw-file)
 (ess-execute Sw-cmd 'buffer nil nil)
 (switch-to-buffer rnw-buf)
 (ess-show-buffer (buffer-name sbuffer) nil)))))


(defun ess-swv-weave2 ()
 "Run Sweave on the current .Rnw file."
 (interactive)
 (ess-swv-run-in-R2 "Sweave"))

(define-key noweb-minor-mode-map "\M-nw" 'ess-swv-weave2)

Now, I can do M-n w to Sweave with cacheSweaveDriver(). Tried it, didn't work. After many trials, i decided to put this code in my .emacs file. Works. Hmm, wierd. Is ESS using a different ess-swv.el file? Tried deleting it from where i think it is. M-n s still works. Weird!!!

After some more searching, found out that the stuff is going on in the ess-swv.elc file. Deleted both files, M-n s doesn't work anymore. Googled what *.elc is. It is a byte-compiled version of .el (to make things run faster in emacs, sometimes). So I did it to mine.

Byte compile for emacs: [http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs\_26.html](http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs_26.html)

So I open my ess-swv.el file in emacs, typed M-x byte-compile-file

Voila! .elc file created. Close emacs and run it again!