Skip to content

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

2 messages · Martin Maechler, Peter Dalgaard

#
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)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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 <maechler@stat.math.ethz.ch> writes:
^^^^^^^^^^^
                           on.exit(options(oo))
I'm not sure this is a bug at all, given the scoping rules, etc.
However, things are strange. Consider the following:
[1] 2
[1] 2
[1] 2
[1] 3.141593

The difference is that in the first case, a is in .GlobalEnv, and not
modifiable with "<-" ("<<-" works). Apparently, "$<-" won't
automatically create a copy of a in the current environment which is
what I had expected. Still, creating .Options explicitly in tst()
doesn't work, which fact is probably due to the digits argument in
print.default defaulting to NULL, and print.default has no reason to
look on the frame stack for .Options.

However, changing the print.default definition to

print.default<-function (x, digits = eval(expression(.Options$digits),
sys.frame(sys.parent())), quote = TRUE, na.print = NULL, print.gap = NULL) 
{
        .Internal(print.default(x, digits, quote, na.print, print.gap))
}

*and*

tst<-function (x = pi, dig = 3) 
{
        .Options <- .Options
        .Options$digits <- as.integer(dig)
        print(x)
        x
}


does seem to do what you wanted...