On Sat, 16 Jan 2010, Ista Zahn wrote:

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

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:

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

foo <- function() {fooval <- 1;Sweave("test2.Rnw")}
cat("@ \n<<ls>>=\nls(env=sys.frame(1))\n@ %def\n",file='test2.Rnw')
foo()
Writing to file test2.tex
Processing code chunks ...
 1 : echo term verbatim (label=ls)

You can now run LaTeX on 'test2.tex'
cat(readLines("test2.tex"),sep='\n')
\begin{Schunk}
\begin{Sinput}
ls(env = sys.frame(1))
\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



-Ista



On Sat, Jan 16, 2010 at 3:35 PM, Jan-Henrik Pötter
<henrik.poet...@gmx.de> wrote:
Hello,

I wrote a function create_report that calls the function Sweave.
create_report shall be used to create a pdf-report about an lm-Object which
is passed as an argument to create_report.

I try to use an object sel_model, that is created within the function
environment of create_report and later on used within the first code chunk
of the file report_template.rnw.

When I use following code I get an error message that the object sel_model
could not been found. How can I achieve to use objects within code-chunks
that are not globally available, but only within my function call of
create_report? Is it only possible to use globally available objects within
a code chunk?



create_report<-function(model_lst){

#define parameters

sel_model<-model_lst

Sweave('report_template.rnw')

tools::texi2dvi("report_template.tex", pdf=TRUE)

}



create_report(p3)





this is the file report_template.rnw



\documentclass[a4paper]{article}

\begin{document}



<<echo=FALSE>>=

summary(sel_model)

@

\begin{center}

<<fig=TRUE,echo=FALSE>>=

plot_fx(sel_model)

@

\end{center}



\end{document}




       [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to