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

Pfaff, Bernhard Dr. Bernhard_Pfaff at fra.invesco.com
Thu Nov 12 14:51:36 CET 2009


>
>On 11/11/2009 12:09 PM, Pfaff, Bernhard Dr. wrote:
>> 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?
>
>The way you did it is close.  You can attach all the local 
>variables by 
>using
>
>attach(environment())
>
Dear Duncan,

many thanks for your swift reply and pointers.

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

sure.

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

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

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

>Duncan Murdoch
>
>> 
>> 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}}
>> 
>> ______________________________________________
>> 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.
>
>




More information about the R-help mailing list