Yet again:Thank you Peter and Duncan. I appreciate your comments and insights.
I agree wholeheartedly with Peter's comments below about understanding what a parsed expression is in R. In R -- and in functional programming in general, I believe -- computing on the language is extremely handy, even for relatively basic programming. I found V&R's discussion in "S Programming" on "Computing on the Language" very helpful in this regard, especially their table 3.2, which dissects the different but cognate situation of formulas, functions, and function calls. Perhaps something similar could be done for expression(), parse(), and quote(). I think there are subtleties here that deserve explicit mention and elaboration. As a feeble attempt in this direction, which I hope illuminates more than it obfuscates -- and I would greatly appreciate prompt corrections of any errors -- perhaps the following might be useful: ######################## a <- 3 x <- quote(a+2) y <- parse(text = "a+2") z <- expression(a+2) ## eval(x) ## 5 eval(y) ## the same eval(z) ## the same ## ## Now note: as.list(x) ## quote(a+2) the parse tree for the expression a+2 ## as.list(y) ## looks like a list whose component is the parse tree for the expression ## as demonstrated by: ## identical(x,y[[1]]) ##TRUE ## as.list(z) ## appears to be the same as z. And, indeed, ## identical(y[[1]],z[[1]]) ##TRUE. The parse tree for the expression again. ## ### However ## identical(y,z) ## FALSE !! ## To see what's going on, use str() str(x) ## language a + 2 str(y[[1]]) ## the same str(z[[1]]) ## the same ## ## But str(y) ## complex structure with attributes str(z) ## simple expression ########################################## To me, this reinforces Bill Dunlap's and Thomas Lumley's counsels: avoid explicit parsing and evaluation via eval(parse(...)) in favor of working with the parsed expression via substitute() and bquote(). HTH, Bert On Mon, May 21, 2012 at 12:20 AM, peter dalgaard <pda...@gmail.com> wrote: > > On May 21, 2012, at 05:25 , Duncan Murdoch wrote: > >> On 12-05-20 10:28 PM, Bert Gunter wrote: >>> Well, that's not very comforting, Duncan. It's like saying that you >>> have to read the engineering specs to drive the car successfully. >> >> I think Robert's message that I responded to was asking for a deeper >> understanding than simply driving the car. He appeared to want to know why >> the car worked the way it did, and describing that entirely in terms of >> things you can see without opening the hood is hard. > > There are levels, though. For basic car driving, it might be sufficient to > know that turning the steering wheel left makes the car change direction > towards left. Rather soon, you will realize that it is imortant that it does > so by turning the front wheels; this explains why you need to reverse into a > parallel-parking space. At some point, it may become useful to know that the > wheels are tangential to the curve that the car follows and that it therefore > turns around a point on the line trough the rear wheels (not that that ever > helped me to parallel park...). > > In R, it is important to have some reasonably accurate mental image of its > internal structures. For quote() and friends, the thing that you really need > is the notion of a _parse tree_, i.e. the fact that expressions are not > evaluated as-is, but first converted (parsed) to an internal structure that > is equivalent to a list of lists: > >> e <- quote(2/(3+a)) >> e[[1]] > `/` >> e[[2]] > [1] 2 >> e[[3]] > (3 + a) >> e[[3]][[1]] > `(` >> e[[3]][[2]] > 3 + a >> e[[3]][[2]][[1]] > `+` >> e[[3]][[2]][[2]] > [1] 3 >> e[[3]][[2]][[3]] > a > > or, graphically (mailer permitting) > > `/` +--2 > | > +--`(`--`+` +-- 3 > | > +-- a > > Once you have this concept in mind, it should become fairly clear that the > string constant "a" is fundamentally different from the variable name a. > > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Email: pd....@cbs.dk Priv: pda...@gmail.com > > > > > > > > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm ______________________________________________ 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.