[R] SWeave - can I see output in the source?

Duncan Murdoch murdoch at stats.uwo.ca
Sat Nov 19 15:52:22 CET 2005


On 11/19/2005 6:13 AM, Friedrich.Leisch at tuwien.ac.at wrote:
>>>>>>On Fri, 18 Nov 2005 13:49:36 -0500,
>>>>>>Duncan Murdoch (DM) wrote:
> 
> 
>   > I'm working on a Latex document with lots of R code in it, so naturally 
>   > enough it would be a good idea to use SWeave.  But then I don't get to 
>   > see the output as I'm editing.
> 
>   > Or do I?  Is there a tool to process a .Rnw file and incorporate the 
>   > output from the commands into it (in a form that is not used for 
>   > producing the output .tex file, but which is updated each time I process 
>   > the file)?
> 
> I'm not sure if I understand the question correctly, but if you edit
> Sweave files in Emacs using ESS you can send the code lines to a
> running R process, and there you see the output. At least that's how I
> write my Sweave files.
> 
> When I want to see all at once I typically do a tangle & source.

That's bad news for me, because I'm allergic to Emacs.  What I had in 
mind was this:  In my .Rnw file, I enter:

<<echo=true>>=
1:3
@

Then I pass it to some tool, which modifies the .Rnw file, changing it 
to something like

<<echo=true>>=
1:3
@
% > 1:3
% [1] 1 2 3

(My editor will notice that the tool has changed the file and offer to 
load the new version at this point.  I think that's a reasonably common 
editor option.)

Then I can see what I'm writing about when I describe the results.  If I 
later come along and edit the source to change it to

<<echo=true>>=
1:4
@
% > 1:3
% [1] 1 2 3

then the next time I run the file through the tool it will delete the 
stale output and modify my file to look like this:

<<echo=true>>=
1:4
@
% > 1:4
% [1] 1 2 3 4

Besides the advantage that I had in mind (being able to see the output 
as I'm editing, and being confident that it will match the output in the 
final document), this will mean that I'll have a versionable record of 
what the output looked like (so I'll be alerted to changes in it caused 
by updates to R or some package I'm using).  I could get this by saving 
the .tex output, but to me this seems preferable.  But I don't have a 
lot of experience with SWeave yet, so maybe there's a better workflow.

Duncan Murdoch




More information about the R-help mailing list