[R] SPlus script

William Dunlap wdunlap at tibco.com
Sat Jun 8 02:47:14 CEST 2013


> Very interesting.  But what explicitly happens with ***source()***
> in Splus???

Here are some examples where I use the exprs argument instead of making a
tempory file and source the file.  I don't know what you are looking for.
(The OP never showed any calls to the functions in defined in his script, so
I don't know what he was doing either.)

  S+>  source( exprs = expression( 12:15, invisible(1/9), log2(1:10)), auto.print=TRUE)
  S+> 12:15
  [1] 12 13 14 15
  S+> invisible(1/9)
  S+> log2(1:10)
   [1] 0.000000 1.000000 1.584963 2.000000 2.321928 2.584963 2.807355 3.000000
   [9] 3.169925 3.321928

  S+> source( exprs = expression( 12:15, invisible(1/9), log2(1:10)), auto.print=TRUE, echo=FALSE)
  [1] 12 13 14 15
   [1] 0.000000 1.000000 1.584963 2.000000 2.321928 2.584963 2.807355 3.000000
   [9] 3.169925 3.321928

  S+> source( exprs = expression( { 12:15 ; log2(1:10) } ), auto.print=TRUE)
  S+> {
          12:15
          log2(1:10)
  }
   [1] 0.000000 1.000000 1.584963 2.000000 2.321928 2.584963 2.807355 3.000000
   [9] 3.169925 3.321928

The next doesn't autoprint because invisible() sets the autprint flag to FALSE in the
caller's frame:
  S+> source( exprs = expression( { 12:15 ; invisible(1/9) ; log2(1:10) } ), auto.print=TRUE)
  S+> {
          12:15
          invisible(1/9)
          log2(1:10)
  }


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: Rolf Turner [mailto:rolf.turner at xtra.co.nz]
> Sent: Friday, June 07, 2013 4:46 PM
> To: William Dunlap
> Cc: r-help at r-project.org
> Subject: Re: [R] SPlus script
> 
> 
> Very interesting.  But what explicitly happens with ***source()***
> in Splus???
> 
>      cheers,
> 
>          Rolf
> 
> On 08/06/13 11:33, William Dunlap wrote:
> >>       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.



More information about the R-help mailing list