On 28/05/2010 1:58 PM, Paul wrote:> Hello
> It seems that sweave always runs in the global environment.
By default it uses the RweaveEvalWithOpt function to evaluate
expressions, and they are evaluated in the global environment. It's
possible to change that. You need to make your own "driver". I'd
start
with the default one via
mydriver <- RweaveLatex()
then replace the code running part with your own, e.g.
myenvironment <- environment() # This will make the current environment
is the default, rather than globalenv()
myEvalWithOpt <- function (expr, options){
if(options$eval){
res <- try(withVisible(eval(expr, myenvironment)),
silent=TRUE)
if(inherits(res, "try-error")) return(res)
if(options$print | (options$term & res$visible))
print(res$value)
}
return(res)
}
mydriver$runcode <- makeRweaveLatexCodeRunner(evalFunc = myEvalWithOpt)
and then run Sweave with your driver:
Sweave(file, driver=mydriver)
I haven't tried any of this, and it's possible some of the functions I
used above are not exported from the utils package. But this should get
you started if you (sensibly) want to avoid setting your variable as a
global.
Another approach (which I use) is to never run Sweave() from within R;
always use R CMD Sweave (or some equivalent), and define all the local
variables in the Sweave file. But this doesn't work if you want to
generate lots of Sweave output files.
Duncan Murdoch
> I want to
> run sweave from within a function, and pass a variable into sweave,
> however when I do this, sweave doesn't see the variable.
>
> Here's my example test_sweave.Rnw file
>
> |%
> \documentclass[a4paper]{article}
> \usepackage[OT1]{fontenc}
> \usepackage{Sweave}
> \begin{document}
>
> \title{Test Sweave Document}
> \author{Paul Hurley}
>
> \maketitle
>
> <<>>>
> if(exists("foo")){print(foo)}
> ls()
> Sys.time()
> @
> \end{document}
> |
>
> If I run this code;
>
> |testFoo<-function(){
> foo<-"My Test String"
> Sweave("test_sweave.Rnw")
> require(tools)
> texi2dvi(file = "test_sweave.tex", pdf = TRUE)
> }
>
> rm(foo) testFoo()
> |
>
> my resulting file does NOT contain the contents of the string foo.
>
> |> if (exists("foo")) {
> + print(foo)
> + }
> > ls()
> [1] "testFoo"
> |
>
> If I run this code (i.e, the same thing, just run directly)
>
> |rm(foo)
> foo<-"My Test String"
> Sweave("test_sweave.Rnw")
> require(tools)
> texi2dvi(file = "test_sweave.tex", pdf = TRUE)
> |
>
> my resulting file does contain the foo string
>
> |> if (exists("foo")) {
> + print(foo)
> + }
> [1] "My Test String"
> > ls()
> [1] "foo" "testFoo"
> |
>
> and if I run this code
>
> |testBar<-function(){
> foo<<-"My Test String"
> Sweave("test_sweave.Rnw")
> require(tools)
> texi2dvi(file = "test_sweave.tex", pdf = TRUE)
> }
>
> rm(foo)
> testBar()
> |
>
> My resulting file also contains the foo string
>
> |> if (exists("foo")) {
> + print(foo)
> + }
> [1] "My Test String"
> > ls()
> [1] "foo" "testBar" "testFoo"
> |
>
> So, it seems that sweave runs in the global environment, not in the
> environment it was called from. This means the only way to pass
> variables to sweave when sweave is run from a function is to use the
<<-
> operator to put the variable in the global environment. (I think).
>
> Anyone else want to comment who knows more about environments ?
>
>
> Thanks
>
> Paul.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>