Skip to content
Prev 324972 / 398502 Next

SPlus script

This is true in both R and S+.  Only the return value of a function is available
for autoprinting and sin(42) is not the return value.  Perhaps you are remembering
the case where the last subexpression in the function is an assignment, in which
case S+ autoprints its value but R does not.

  R> f <- function(x) { y <- x + 1 }
  R> f(10)
  R> .Last.value
  [1] 11

  S+> f <- function(x) { y <- x + 1 }
  Warning messages:
    Last expression in function is an assignment
                  (You probably wanted to return the left-hand side)
           in: y <- x + 1
  S+> f(10)
  [1] 11
  S+> .Last.value
  [1] 11

Autoprinting is implemented quite differently in R and S+.  I think R attaches
the autoprint flag to the returned object while S+ assigns .Auto.print<-TRUE
in the frame of the caller (yuck).  That leads to differences like the following

  R> f <- function(x) invisible(x+1)
  R> g <- function(x) 10 * x
  R> g(f(23)) # g() and f() are evaluated in same frame
  [1] 240
  R> .Last.value
  [1] 240

  S+> f <- function(x) invisible(x+1)
  S+> g <- function(x) 10 * x
  S+> g(f(23)) # g() and f() are evaluated in same frame
  S+> .Last.value
  [1] 240

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com