Skip to content

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

2 messages · Martin Maechler, Bill Venables

#

        
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)

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

ok;
yes, I wonder too, how S-plus does it. (Bill V. ?)
    
    Robert>   Since the method Martin so capably demonstrated with tst2
    Robert> does work (except I think he meant on.exit(options(oo)) rather
    Robert> than on.exit(oo)) I think we should stick with that. In that
    Robert> case the global value of options(digits) is changed for the
    Robert> duration of the function evaluation of tst2.
   
okay, I could live with it.

Problem is:

	In the current version (i.e. 0.50-a3), even  tst2(.)
						     ---^---
	does NOT work !
	(yes, it did before ..)

- Martin
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
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
}
[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
operates like an assignment sequence of the form
Which you normally cannot do directly, of course, (and clearly
the effect is not permanent).

Bill.