[R] Sweave() within a function: objects not found

Pfaff, Bernhard Dr. Bernhard_Pfaff at fra.invesco.com
Wed Nov 11 18:09:36 CET 2009


Dear list subscriber,

suppose, I do have a minimal Sweave file 'test.Rnw':
\documentclass{article}
\begin{document}
<<printx>>=
x
@ 
\end{document}


Within R, I define the following function:

f <- function(x){
  Sweave("test.Rnw")
}

The call:

f(x = 1:10)

results in the following error message:

> f(x = 1:10)
Writing to file test.tex
Processing code chunks ...
 1 : echo term verbatim (label=printx)

Error:  chunk 1 (label=printx) 
Error in eval(expr, envir, enclos) : object 'x' not found

In principle, I could assign x to the global environment and then the Sweave file will be processed correctly:

> f2 <- function(x){
+   attach(list(x = x))
+   Sweave("test.Rnw")
+ }
> f2(x = 1:10)
Writing to file test.tex
Processing code chunks ...
 1 : echo term verbatim (label=printx)

You can now run LaTeX on 'test.tex'
> 

Kind of a dum question, but how could it be achieved that Sweave recognizes the objects within this function call?

Any pointers are most welcome,
Bernhard

> sessionInfo()
R version 2.10.0 (2009-10-26) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  datasets  utils     grDevices methods   base     

other attached packages:
[1] fortunes_1.3-6
> 

Dr. Bernhard Pfaff
Director
Global Quantitative Equity

Invesco Asset Management Deutschland GmbH
An der Welle 5
D-60322 Frankfurt am Main

Tel: +49 (0)69 29807 230
Fax: +49 (0)69 29807 178
www.institutional.invesco.com
Email: bernhard_pfaff at fra.invesco.com

Geschäftsführer: Karl Georg Bayer, Bernhard Langer, Dr. Jens Langewand, Alexander Lehmann, Christian Puschmann
Handelsregister: Frankfurt am Main, HRB 28469
Sitz der Gesellschaft: Frankfurt am Main

*****************************************************************
Confidentiality Note: The information contained in this ...{{dropped:10}}




More information about the R-help mailing list