Skip to content

Sweave() within a function: objects not found

3 messages · Pfaff, Bernhard Dr., Duncan Murdoch

#
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:
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:
+   attach(list(x = x))
+   Sweave("test.Rnw")
+ }
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
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}}
#
On 11/11/2009 12:09 PM, Pfaff, Bernhard Dr. wrote:
The way you did it is close.  You can attach all the local variables by 
using

attach(environment())

though global variables will take precedence, because attach puts the 
environment 2nd in the search list.  And you'd better remember to detach 
them.

I'd say it's better to make Sweave files self-contained, so that you can 
run R CMD Sweave outside of R, and get the right results.  But if you 
really want to do this, then you can write your own Sweave driver and 
replace the default  RweaveEvalWithOpt  with a function that looks 
elsewhere for variables.

Duncan Murdoch
#
Dear Duncan,

many thanks for your swift reply and pointers.
sure.
Right, this is ordinarily the way I proceed, too. But, now, I would like to include "ready-made" Sweave reports in a package, e.g. /inst/reports/template1.Rnw, and provide a function, say reportgen(), with which a user can be provide his objects that are then used within the Sweave file(s).

But if you
Having said the above, I think that this will be the route to take and indeed in RweaveEvalWithOpt is the line:

res <- try(withVisible(eval(expr, .GlobalEnv)), silent = TRUE)

which needs to be adjusted.

Best,
Bernhard