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

Friedrich.Leisch@tuwien.ac.at Friedrich.Leisch at tuwien.ac.at
Sun Nov 20 15:52:53 CET 2005


>>>>> On Sat, 19 Nov 2005 09:52:22 -0500,
>>>>> Duncan Murdoch (DM) wrote:

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

Well, the origin of Sweave is that I found Tony Rossini's ESS-noweb
mode for Emacs really neat and started to explre what you can do with
it ...


  > 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

Shouldn't be too hard to code (apart from the fact that you cannot
insert figures as easily as text), but I personally am unlikely to do
it because it is completely orthogonal to my style of interacting with
Sweave documents. When I edit them I always want them to be connected
to a running R process to easily try out modifications etc.

Ad emacs vs other editors: well, we "just" would need another editor
that is connected to a running R process, i.e., can easily send code
lines to R, and there are a couple of those. Emacs will know when you
are in a code chunk and when you are in a doc chunk, and lets you
execute code only in those cases (including sending all of the current
chunk to R). It also does syntax highlighting (R vs LaTeX) in
dependence on the 

But if the user knows when to press the corresponding button a simple
solution might be to simply turn of the R connection for LaTeX files
in another editor than Emacs. 



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


That is something I have alraqdy thought a little bit about, i.e., to
have mechanisms for easily comparing the processed versions of Sweave
files and see if something has changed. The tricky bit are (as always
with regression tests) the figures, and I have no simple solution for
that yet. Started playing with Paul's GraphicsQC package, but found no
time for serious coding yet.

Without checking figures a simple diff on the .tex files so far worked
fine for me ...


Best,
Fritz




More information about the R-help mailing list