Hello It seems that sweave always runs in the global environment. 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@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.