From berwin at maths.uwa.edu.au  Fri Dec  2 11:31:13 2005
From: berwin at maths.uwa.edu.au (Berwin A Turlach)
Date: Fri, 2 Dec 2005 18:31:13 +0800
Subject: [Rd] Enlightenment sought and a possible buglet in vector.Rd
Message-ID: <17296.8817.937646.30624@bossiaea.maths.uwa.edu.au>

Dear all,

First, I recently had reasons to read the help page of as.vector() and
noticed in the example section the following example:

     x <- c(a = 1, b = 2)
     is.vector(x)
     as.vector(x)
     all.equal(x, as.vector(x)) ## FALSE

However, in all versions of R in which I executed this example, the
all.equal command returned TRUE which suggest that either the comment
in the help file is wrong or the all.equal/as.vector combination does
not work as intended in this case.  For the former case, I attach
below a patch which would fix vector.Rd.

Secondly, I stumbled across two behaviours of R that I cannot explain
but would like to know why R behaves as it does.  But since I expect
the explanations to be quite technical, I though that r-devel is the
more appropriate list to ask on than r-help.

The first example is the following:

       > f1
       function(){
           par.def <- par(no.readonly=TRUE)
           on.exit(par(par.def))
           tt <- sys.on.exit()
           print(tt)
           str(tt)
           invisible()
         }
       > f1()
       par(par.def)
        language par(par.def)
       > f2
       function(){
           par.def <- par(no.readonly=TRUE)
           on.exit(par(par.def))
           print(tt <- sys.on.exit())
           str(tt)
           invisible()
         }
       > f2()
       NULL
        NULL

I found in the R language definition manual the passage that
discourages users of assigning objects within function calls since it
is not guaranteed that the assignment is ever made because of R's lazy
evaluation model.  But this does not seem to explain the above
behaviour since the argument to print is evaluated.  If I replace
sys.on.exit() with, say, ls() in both functions, then they produce the
same output (and the output that I expect).  Why does f2() not work
with sys.on.exit()?

The second behaviour that I cannot explain was produced by code
written by somebody else, namely: 

      > foo
      function(x){
          z <- x/4
          while( abs(z*z*z-x) > 1e-10 ){
             z <- (2*z+x/z^2)/3
          }
      }

The documentation of function() says that if "the end of a function is
reached without calling 'return', the value of the last evaluated
expression is returned."  And this seems to happen in this case:

      > z <- foo(3)
      > z
      [1] 1.442250

However, my understanding was always that the return value of a
function issued on the command line will be printed; except, of
course, if invisible() is used to return the value.  This is not the
case for the above function:

      > foo(3)

produces no output.  And this had us stunned for some time.  On the
other hand:

      > ( foo(3) )
      [1] 1.442250

So my question is why does R, when "foo(3)" is issued on the command
line, not print the value returned by the function?

Any enlightening comments are highly appreciated.

Cheers,

        Berwin


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: R-patch
Url: https://stat.ethz.ch/pipermail/r-devel/attachments/20051202/5340f532/R-patch.pl


[Rd] Enlightenment sought and a possible buglet in vector.Rd
Berwin A Turlach