Skip to content

Sweave - How to use local objects in code chunks

2 messages · Ista Zahn, Charles C. Berry

#
I don't think it works that way. The .Rnw file should be
self-contained. Put all the R code in the .Rnw file (or keep it in a
separate file and source() it into the .Rnw file).

-Ista



On Sat, Jan 16, 2010 at 3:35 PM, Jan-Henrik P?tter
<henrik.poetter at gmx.de> wrote:

  
    
#
On Sat, 16 Jan 2010, Ista Zahn wrote:

            
Right. So the issue is to figure out how to do this in a way that allows 
Jan-Henrik access to objects in a function calling Sweave().

One way is to look at the frames/calls that contain the code chunk in 
question.

See

 	?sys.status

for some background.

Then try something like this:

=========================================================
Writing to file test2.tex
Processing code chunks ...
  1 : echo term verbatim (label=ls)

You can now run LaTeX on 'test2.tex'
\begin{Schunk}
\begin{Sinput}
\end{Sinput}
\begin{Soutput}
[1] "fooval"
\end{Soutput}
\end{Schunk}

=========================================================

As you see, 'fooval' which was defined in foo() is found in sys.frame(1).

This might be good enough for Jan-Henrik.

But generally, what you need to do is to figure out which frame contains 
the object you want your *.Rnw doc to find (since foo() could have been 
called by another function).

You might write a chunk that will look at sys.calls(), find which element 
corresponds to 'foo()' and go from there.

HTH

Chuck
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901