From wvenable@attunga.stats.adelaide.edu.au  Thu Aug 14 06:13:11 1997
From: wvenable@attunga.stats.adelaide.edu.au (Bill Venables)
Date: Thu, 14 Aug 1997 14:43:11 +0930
Subject: R-alpha: .Options$digits  do not (always) work.
In-Reply-To: <199708131037.MAA00520@sophie.ethz.ch>
References: <199708122358.LAA04005@stat1.stat.auckland.ac.nz>
 <199708131037.MAA00520@sophie.ethz.ch>
Message-ID: <9708140513.AA13924@attunga.stats.adelaide.edu.au>

Martin Maechler writes:
 > >>>>> "Robert" == Robert Gentleman <rgentlem@stat.auckland.ac.nz> writes:
 > 
 >     Robert> 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)

I think not.  Here is what you probably meant to use

tst2 <- function(x = pi, dig = 3) {
  oo <- options(digits = dig)
  on.exit(options(oo))
  print(x)
  x
}

> tst2()
[1] 3.14
[1] 3.14159

Notice that the on.exit() happens after the function is complete
but before auto-printing.

 >     Robert> Well it isn't really clear what Splus does to get
 >     Robert> tst to work. It appears that they use some form of
 >     Robert> dynamic scope for Options.  Clearly, the
 >     Robert> assignment, .Options$digits <- xxxx creates a local
 >     Robert> copy of .Options and changes the value of
 >     Robert> that.  The only way that print can find this local
 >     Robert> copy is if it looks up the stack of calling
 >     Robert> functions to find it (or perhaps Splus squirrels
 >     Robert> it away somewhere that print looks but then <-
 >     Robert> starts to behave in a very peculiar manner).  This
 >     Robert> sort of behaviour is bad for two reasons 1) We
 >     Robert> really want people to use lexical scope and any
 >     Robert> examples where you have dynamic scope simply
 >     Robert> confuse things 2) It is quite inefficient for
 >     Robert> print (and anything else that uses Options) to
 >     Robert> have to look up the stack of calling functions.
 > 
 > ok;
 > yes, I wonder too, how S-plus does it. (Bill V. ?)

(? Y me?) Sorry, but I don't have any inside knowledge.  A little
experimentation shows that

> options(digits = 10)

operates like an assignment sequence of the form

> .Options$digits <- 10
> assign(".Options", .Options, where = 6)  # or wherever...
> remove(".Options")

Which you normally cannot do directly, of course, (and clearly
the effect is not permanent).

Bill.

-- 
Bill Venables, Head, Dept of Statistics,    Tel.: +61 8 8303 5418
University of Adelaide,                     Fax.: +61 8 8303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables@adelaide.edu.au
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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.
Bill Venables