[R] Lexical scoping is not what I expect

William Dunlap wdunlap at tibco.com
Sat Jun 29 16:25:13 CEST 2013


> > assign('TRUE', FALSE)
> > TRUE
> [1] TRUE
> > get('TRUE')
> [1] FALSE
> 
> but it is probably a different story.

The parser converts the text
   TRUE
to the value true, not to the name 'TRUE', just as it converts the text
   1
to the value one, not the name '1', before handing off the parse tree
to the evaluator, so scoping rules are not relevant.  You can also use
backquotes to force the TRUE or 1 to be interpreted as names so that
scoping rules are used.

  R> class(parse(text="TRUE")[[1]])
  [1] "logical"
  R> class(parse(text="`TRUE`")[[1]])
  [1] "name"
  R> class(parse(text="1")[[1]])
  [1] "numeric"
  R> class(parse(text="`1`")[[1]])
  [1] "name"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Yihui Xie
> Sent: Friday, June 28, 2013 7:12 PM
> To: Rolf Turner
> Cc: r-help at r-project.org; John Fox
> Subject: Re: [R] Lexical scoping is not what I expect
> 
> I just realized this was also possible:
> 
> > assign('TRUE', FALSE)
> > TRUE
> [1] TRUE
> > get('TRUE')
> [1] FALSE
> 
> but it is probably a different story.
> 
> Regards,
> Yihui
> --
> Yihui Xie <xieyihui at gmail.com>
> Phone: 206-667-4385 Web: http://yihui.name
> Fred Hutchinson Cancer Research Center, Seattle
> 
> 
> On Fri, Jun 28, 2013 at 6:30 PM, Rolf Turner <rolf.turner at xtra.co.nz> wrote:
> > On 29/06/13 02:54, John Fox wrote:
> >>
> >> Dear Duncan and Steve,
> >>
> >> Since Steve's example raises it, I've never understood why it's legal to
> >> change the built-in global "constants" in R, including T and F. That just
> >> seems to me to set a trap for users. Why not treat these as reserved
> >> symbols, like TRUE, Inf, etc.?
> >
> >
> > I rather enjoy being able to set
> >
> >     pi <- 3
> >
> > :-)
> >
> >     cheers,
> >
> >         Rolf Turner
> 
> ______________________________________________
> 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