Derek Eder
2002-Sep-06 12:58 UTC
[R] Scope problems - passing an argument from one function to another (LME) within
I am having trouble understanding what must be a quite simple rule of scope. In the code example below, the argument to the main function, my.subset, would like to be passed to the function LME. This does not happen however. Curiously, the other argument (my.data) is passed to LME. This problem goes away with this: assign("my.subset", my.subset, env = sys.frame()). It works, but it doesn't feel right. How can I get LME to join the scope of the main function? Thank you, Derek Eder my.function_function(my.data,my.subset="NORM") { ## call to function LME. Problem: the variable my.subset is not seen by LME ## # assign("my.subset", my.subset, env = sys.frame()) Now it works! # my.result_lme( X ~ Y, random=~1|Z, data=my.data, subset= W==my.subset) } Derek N. Eder G?teborgs Universitet Institutionen f?r klinisk neurovetenskap Klinisk Neurofysiologi Sahlgrenska universitetssjukhuset SS/SU Bl? straket 7, v?n 3 SE 413 45 G?teborg Sverige Tlf. +46 (031) 34 2-6139 (office) Tlf. +46 (031) 34 2-1283 (laboratory) Tlf. +46 0709 / 7 2-1283 (mobil) Fax. +46 (031) 82 81 63 derek.eder at neuro.gu.se Gothenburg University Institute of Clinical Neuroscience, Department of Clinical Neurophysiology Salhgrenska Hospital SU/SS SE 413 45 G?teborg Sweden -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thomas Lumley
2002-Sep-06 15:01 UTC
[R] Scope problems - passing an argument from one function to another (LME) within
On Fri, 6 Sep 2002, Derek Eder wrote:> I am having trouble understanding what must be a quite simple rule of > scope. > > In the code example below, the argument to the main function, my.subset, > would like to be passed to the function LME. This does not happen > however. Curiously, the other argument (my.data) is passed to LME.Yes. Modelling and graphics functions with formulas all have slightly different nonstandard evaluation rules, and it turns out that lme() doesn't evaluate extra arguments like subset in the parent environment. There's a description of the nonstandard evaluation rules on developer.r-project.org, but I will have to rewrite it to include lme, which I didn't know about.> This problem goes away with this: assign("my.subset", my.subset, env > sys.frame()). > > It works, but it doesn't feel right. How can I get LME to join the > scope of the main function?You can't, not easily. If you specify a data frame then lme() expects it to contain all the variables, if you don't specify a data frame then it looks in the local environment. help(lme) is consistent with this, but if you did't know you probably wouldn't interpret it that way. It turns out that it always looks in the global environment as well, so you only notice this inside a function -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._