[ESS] Is ess supposed to help with Sweave documents?

David Whiting david.whiting at ncl.ac.uk
Tue Jan 25 08:17:04 CET 2005

On Mon, Jan 24, 2005 at 08:14:52PM +0100, A.J. Rossini wrote:
> If you are using Sweave using strictly Noweb markup, then the fast and
> painful way is to use a Makefile to extract the code with noweb and
> dump it to a file, then reload it.  But you've got to be using markup
> which is in the intersection of Noweb and Sweave (and I'm not going to
> go into that argument again).
> Another option is to use the submit thread command (I don't think it's
> bound) which creates a temp buffer of the "thread" (i.e. appropriate
> chunks strung together) and dumps it into a buffer.

Does this get you nearly there Henrik? If you have a .Rnw file open
and call this function it will extract the source code (making
assumptions about markup?), open the source code file and then load
it. If there is an error you will see the location of the error in the
*ESS-errors* buffer.  At the moment this function does not
automatically switch to the buffer on error (and I don't think that I
will have time to add this just now). But at least you don't need to
create a Makefile.  You could bind this to a key combination and
almost use it as simply a C-c C-l.

(defun ess-R-code-only ()
  "Extract R source code from a Rnw file and load it."
    ;; Get the name of the file we are working with.
    (setq namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
    (setq R-filename (concat namestem ".R"))
    ;; Make sure tools is loaded.
    (setq ess-command (format "library(tools)"))
    (ess-execute ess-command)
    ;; Extract the source code.
    (message "Stangling %S" (buffer-file-name))
    (setq ess-command (format "Stangle(%S)" (buffer-file-name)))
    (ess-execute ess-command 'buffer nil nil)
    ;; Open the source code file.
    (find-file R-filename)
    ;; Load the source code into R.
    (ess-load-file R-filename)))


David Whiting
School of Clinical Medical Sciences, The Medical School
University of Newcastle upon Tyne, NE2 4HH, UK.

More information about the ESS-help mailing list