[R] SPlus script

William Dunlap wdunlap at tibco.com
Sat Jun 8 01:33:45 CEST 2013


>      foo <- function(x) {
>          sin(42)
>          x^2
>      }
> 
>      foo(3)
>      [1] 9
> 
> The value of sin(42) is never seen.

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


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Rolf Turner
> Sent: Friday, June 07, 2013 4:17 PM
> To: Duncan Murdoch
> Cc: r-help at r-project.org; Scott Raynaud
> Subject: Re: [R] SPlus script
> 
> On 07/06/13 23:05, Duncan Murdoch wrote:
> > On 13-06-06 6:22 PM, Rolf Turner wrote:
> >> On 07/06/13 03:19, Scott Raynaud wrote:
> >>> I actually had tried placing arguments in the call but it didn't
> >>> work.   However, I did
> >>> not think about writing it to a variable and printing.  That seems
> >>> to have done the
> >>> trick.  Funny, I don't remember having to do that before, but that's
> >>> not surprising.
> >>>
> >>
> >> If I remember correctly --- haven't used Splus for decades --- this is a
> >> difference
> >> between Splus and R.
> >>
> >> In R the output of a function is returned *invisibly* if that function
> >> is called
> >> from within another function.  And source() is one such other function.
> >
> > Actually this depends on the caller.  source() does return its results
> > invisibly, but many other functions don't.
> 
>  From FAQ 7.16:
> > If you type '1+1' or 'summary(glm(y~x+z, family=binomial))' at the
> > command line the returned value is automatically printed (unless it is
> > |invisible()|), but in other circumstances, such as in a |source()|d
> > file or ***inside a function*** it isn't printed unless you
> > specifically print it.
> 
> (Emphasis added.)
> 
> I think that you have misinterpreted what I wrote.  Many (most?) functions
> *return* their results (values) visibly.  But if you put an expression
> into the code
> of that function (an expression which is not part of the returned value)
> you never
> see the result of evaluating that expression.
> 
> E.g.:
> 
>      foo <- function(x) {
>          sin(42)
>          x^2
>      }
> 
>      foo(3)
>      [1] 9
> 
> The value of sin(42) is never seen.
> 
> The main point however is that IIRC Splus is different from R in respect
> of whether the values of (un-assigned) expressions inside source are
> visible.  In R they are invisible; in Splus I *believe* (vaguely recall)
> that
> they are visible.  I cannot check this since I have no access to Splus.
> 
> I do wish someone would confirm (or deny, as the case may be) that
> my recollections about Splus are correct.
> 
>      cheers,
> 
>          Rolf
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list