From rgentlem@stat.auckland.ac.nz  Wed Aug 13 00:58:51 1997
From: rgentlem@stat.auckland.ac.nz (Robert Gentleman)
Date: Wed, 13 Aug 1997 11:58:51 +1200 (NZST)
Subject: R-alpha: .Options$digits  do not (always) work.
Message-ID: <199708122358.LAA04005@stat1.stat.auckland.ac.nz>

Martin wrote:
>  I am sorry that this IS an old topic.
> Yet another task
> I think the bug is somewhere in hidden  in  src/main/options.c ..
> 
> ##-- The following does not work as it should in  R (0.50-a1, but I think
> 							also earlier)
> 
> tst <- function(x=pi, dig =3) {.Options$digits <- as.integer(dig); print(x);x}
> tst()
> tst(dig = 12)
> 
> 
> ##-- This should do the same; it works as expected in R & S :
> 
> tst2 <- function(x=pi, dig =3) {
>   oo <- options(digits=dig); on.exit(oo); print(x);x}
> tst2()
> tst2(dig = 12)


  Well it isn't really clear what Splus does to get tst to work. It appears
  that they use some form of dynamic scope for Options. Clearly, the 
  assignment,
    .Options$digits<- xxxx
  creates a local copy of .Options and changes the value of that. The only
  way that print can find this local copy is if  it looks up the stack of
  calling functions to find it (or perhaps Splus squirrels it away somewhere
  that print looks but then <- starts to behave in a very peculiar manner).
  This sort of behaviour is bad for two reasons
    1) We really want people to use lexical scope and any examples where you
    have dynamic scope simply confuse things
    2) It is quite inefficient for print (and anything else that uses Options)
    to have to look up the stack of calling functions.
    
  Since the method Martin so capably demonstrated with tst2 does work
   (except I think he meant on.exit(options(oo)) rather than on.exit(oo))
   I think we should stick with that. In that case the global value of 
   options(digits) is changed for the duration of the function evaluation
   of tst2.
   
 robert
 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel 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-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


R-alpha: .Options$digits do not (always) work.
Robert Gentleman