Skip to content

how to get or store the intermediate v?lues while running a function

33 messages · shanmuha boopathy, vincent@7d4.com, Martin Maechler +15 more

Messages 1–25 of 33

#
shanmuha boopathy a ??crit :
may I suggest, inside your function

res = c(k, l, m);
return(res);
# also ... read some intro docs !
#
vincent> shanmuha boopathy a ??crit :
    >> a<-function(a,b,c,d)
    >> {
    >> k=a+b
    >> l=c+d
    >> m=k+l
    >> }
    >> 
    >> in this example the function will return only the value of "m"
    >> ...But I like to extract the values of "l" & "k" also.........
    >> which command to use for storing or for extracting those intermediate value.......

    vincent> may I suggest, inside your function

    vincent> res = c(k, l, m);
    vincent> return(res);

please, please,  these trailing ";"  are  *so* ugly.
This is GNU S, not C (or matlab) !

{and I have another chain of argments why   "<-" is so more
expressive than "="  but I'll be happy already if you could
drop these ugly empty statements at the end of your lines...

    vincent> # also ... read some intro docs !
#
On Tue, 2005-12-06 at 13:43 +0100, Martin Maechler wrote:
#
Martin Maechler wrote:
By the way, does anybody knows if there is a "R tidy" or some similar 
project to automatically reformat (and possibly check) R code, beside 
what Emacs does?
Best,

Philippe Grosjean
#
Yes, it drives me mad too when people use "=" instead of "<-" for
assignment and suppress spaces in an naive attempt for saving space. 

As an example compare 
	
	o=fn(x=1,y=10,z=1)

with

	o <- fn( x=1, y=10, z=1 )

Regards, Adai
On Tue, 2005-12-06 at 13:43 +0100, Martin Maechler wrote:
#
Martin Maechler a ??crit :
May I disagree ?
I find missing ";" at end of lines *so* ugly.
Ugly/not ugly depends on our observer's eyes.
 From my programmer point of view, I prefer to mark
clearly the end of the lines.
In many languages, it's safer to do it this way,
and I thank the R developers to permit it.
(in my opinion, it should even be mandatory).
(By the way, marking the end of lines with a unique symbol
makes also the job easier for the following treatment.)
And yes, I'm also a C programmer ;-)

 > {and I have another chain of argments why   "<-" is so more
 > expressive than "="

Why "<-" seems better than "=" is also quite mysterious for me.
There was a discussion about this point recently I think.
I believe in 99% of cases it's more for historical reason
(and perhaps also for some "snob" reasons).

I am not at all a 20 years experienced R programmer, but I have
written several hundreds of R lines those 6 last months,
and until today didn't get any problem using "=" instead of "<-".

But I'll read your chain of arguments with interest.
#
See the appropriate section in `Writing R Extensions' (3.1 `Tidying R 
code').
#
I consistently use ";" at every end of my R code and have found it much more
neat than those sentences without an end; for "<-" and "=", if I were the
author I would rather take the first representation as a sign of
passing-by-reference while the latter by value.

Xiaofan Li
DAMTP, University of Cambridge, CB3 0WA, UK
Tel +44 7886 614030, Email xl252 at cam.ac.uk


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of vincent at 7d4.com
Sent: 06 December 2005 14:16
To: rHelp
Subject: Re: [R] R is GNU S, not C.... [was "how to get or store ....."]

Martin Maechler a ??crit :
May I disagree ?
I find missing ";" at end of lines *so* ugly.
Ugly/not ugly depends on our observer's eyes.
 From my programmer point of view, I prefer to mark clearly the end of the
lines.
In many languages, it's safer to do it this way, and I thank the R
developers to permit it.
(in my opinion, it should even be mandatory).
(By the way, marking the end of lines with a unique symbol makes also the
job easier for the following treatment.) And yes, I'm also a C programmer
;-)

 > {and I have another chain of argments why   "<-" is so more
 > expressive than "="

Why "<-" seems better than "=" is also quite mysterious for me.
There was a discussion about this point recently I think.
I believe in 99% of cases it's more for historical reason (and perhaps also
for some "snob" reasons).

I am not at all a 20 years experienced R programmer, but I have written
several hundreds of R lines those 6 last months, and until today didn't get
any problem using "=" instead of "<-".

But I'll read your chain of arguments with interest.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.htmle.html
#
On Tue, Dec 06, 2005 at 03:16:17PM +0100, vincent at 7d4.com wrote:
I agree with this view -- I prefer an explicit statement terminator
to a whitespace which terminates if termination is possible, too.
As far as plain, stand-alone assignment statements are concerned, "="
and "<-" are equivalent.

Given the diversity of coding styles that are permitted by R, consistently
using one style is, in practice, perhaps more relevant than finding out
what the "best" style is.

There is a draft R Coding Convention available at

    http://www.maths.lth.se/help/R/RCC/

which may be useful for finding a style that is good because it is
widely used and therefore familiar to a large number of readers.

Best regards, Jan
#
vincent at 7d4.com wrote:

            
Well, I'll have to disagree a bit. While I don't care so much
about trailing ";" (as long as it does not become mandatory),
I don't like the use of "=" for assignment and that's definitely
NOT for "snob" reasons, whatever those are. I just think code is
*much* easier to read if assignment is distinguished from
argument settings.

Peter Ehlers
#
While mucking about with semicolons and line endings I wrote this little 
piece of mildly obfuscated R code:

f1=function(n){

   x =  1
       ---
        n

   return(x)
}

  [best viewed with a proportionally-spaced font]

f1(1) does indeed return 1/1.

Baz
#
I don't put in extraneous ';' because I maybe get a
blister on my little finger.

I suspect that those who find the semi-colons ugly in
R do not find them ugly in C.  I think the reason there
would be a visceral reaction  in R but not in C is that
there is a danger when using them in R that they really
mean something.

We get questions on R-help often enough about why
code like:

if(x > 0) y <- 4
else y <- 4.5e23

doesn't work.

If people habitually used semi-colons, those sorts of
questions would probably multiply.

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
Xiaofan Li wrote:

            
#
Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes:
It doesn't calculate it though... ;-)
#

        
P> vincent at 7d4.com wrote:
>> Martin Maechler a ??crit :
    >> 
    >> 
    >>> please, please,  these trailing ";"  are  *so* ugly.
    >>> This is GNU S, not C (or matlab) !
    >>> 
    >>> but I'll be happy already if you could
    >>> drop these ugly empty statements at the end of your lines...
    >> 
    >> 
    >> May I disagree ?
    >> I find missing ";" at end of lines *so* ugly.
    >> Ugly/not ugly depends on our observer's eyes.
    >> From my programmer point of view, I prefer to mark
    >> clearly the end of the lines.
    >> In many languages, it's safer to do it this way,
    >> and I thank the R developers to permit it.
    >> (in my opinion, it should even be mandatory).
    >> (By the way, marking the end of lines with a unique symbol
    >> makes also the job easier for the following treatment.)
    >> And yes, I'm also a C programmer ;-)
    >> 
    >> > {and I have another chain of argments why   "<-" is so more
    >> > expressive than "="
    >> 
    >> Why "<-" seems better than "=" is also quite mysterious for me.
    >> There was a discussion about this point recently I think.
    >> I believe in 99% of cases it's more for historical reason
    >> (and perhaps also for some "snob" reasons).
    >> 
    >> I am not at all a 20 years experienced R programmer, but I have
    >> written several hundreds of R lines those 6 last months,
    >> and until today didn't get any problem using "=" instead of "<-".
    >> 
    >> But I'll read your chain of arguments with interest.


    P> Well, I'll have to disagree a bit. While I don't care so much
    P> about trailing ";" (as long as it does not become mandatory),
    P> I don't like the use of "=" for assignment and that's definitely
    P> NOT for "snob" reasons, whatever those are. I just think code is
    P> *much* easier to read if assignment is distinguished from
    P> argument settings.

Thank you, Peter.  Indeed, this is exactly the main of my arguments:
Since "=" is used quite often in S for argument setting in
function calls, *additionally* using "<-" for assignment is
more expressive. 
Also, e.g., a2ps (a nice 'ASCII' to PostScript converter), comes
{at least on Debian Linux} preconfigured for R, and uses nice
typesetting for "<-"; similarly for ESS.
OTOH, it's pretty hard to correctly markup and differentiate
those "=" which are assignments from those which are function.
argument settings.

    P> Peter Ehlers

[But really, I'm more concerned and quite bit disappointed by
 the diehard ";" lovers]

Martin Maechler
#
Peter Dalgaard wrote:

            
My previous example is a bit ugly - this one looks nicer:

f1=function(n){

       -1
   x = ---
        n

  return(x)
}

And it returns f(1) as -1/1 and f(-1) as -1/-1 as well.
#
Patrick Burns a ??crit :
I wrote "end of line" in my first message,
but in fact I did mean "end of statement".

By the way, there will always be more ways to make mistakes
than to make rigth ... with or without semi-colons ;-)
#
On 06-Dec-05 Martin Maechler wrote:
Well, while not die-hard, I will put in my own little reason
for often using ";" at the end of lines which don't need them.

Basically, this is done to protect me from myself (so in fact
is quite a strong reason).

I tend to develop extended R code in a "side-window", using
a text editor (vim) in that window, and cut&pasting the
chunks of R code from that window into the R window.
This usually means that I have a lot of short lines,
since it is easier when developing code to work with the
commands one per line, as they are easier to find and
less likely to be "corrected" erroneously.

Finally, when when I am content that the code does the job
I then put several short lines into one longer one.

For example (a function to do with sampling with probability
proportional to weights); first, as written line-by-line:

myfunction <- function(X,n1,n2,n3,WTS){
  N1<-n1;
  N2<-n1+n2;
  N3<-n1+n2+n3;
# first selection
  pii<-WTS/sum(WTS);
  alpha<-N2;
  Pi<-alpha*pii;
  r<-runif(N3);
  ix<-sort(which(r<=Pi));
# second selection
  ix0<-(1:N3);
  ix3<-ix0[-ix];
  ix20<-ix0[ix];
  W<-WTS[ix];
  pii<-W/sum(W);
  Pi<-N1*pii;
  r<-runif(length(Pi));
  ix10<-sort(which(r<=Pi));
  ix1<-ix20[ix10];
  ix2<-ix20[-ix10];
# return the results
  list(X1=X[ix1],X2=X[ix2],X3=X[ix3],ix1=ix1,ix2=ix2,ix3=ix3)
}


Having got that function right, with 'vim' in command mode
successive lines are readily brought up to the current line
by simply pressing "J", which is very fast. This, in the
above case, then results in

MARselect<-function(X,n1,n2,n3,WTS){
  N1<-n1; N2<-n1+n2; N3<-n1+n2+n3;
# first selection
  pii<-WTS/sum(WTS); alpha<-N2; Pi<-alpha*pii;
  r<-runif(N3); ix<-sort(which(r<=Pi));
# second selection
ix0<-(1:N3); ix3<-ix0[-ix]; ix20<-ix0[ix];
  W<-WTS[ix]; pii<-W/sum(W); Pi<-N1*pii;
  r<-runif(length(Pi)); ix10<-sort(which(r<=Pi));
  ix1<-ix20[ix10]; ix2<-ix20[-ix10];
# return the results
  list(X1=X[ix1],X2=X[ix2],X3=X[ix3],ix1=ix1,ix2=ix2,ix3=ix3)
}

The greater readability of the first relative to the second is
obvious. The compactness of the second relative to the first
is evident. Obtaining the second from the first by repeated "J"
is very quick.

BUT -- if I had not put the ";" at the ends of the lines in the
string-out version (which is easy to do as you type in the line
in the first place), then it would be much more trouble to get
the second version, and very easy to get it wrong!

Also, being long used to programming in C and octave/matlab,
putting ";" at the end of a command is an easy reflex, and of
course does no harm at all to an R command.

Not that I'm trying to encourage others to do the same as I
do -- as I said, it's a self-protective habit -- but equally
if people (e.g. me) may find it useful I don't think it should
be discouraged either -- especially on "aesthetic" grounds!

Just my little bit ...

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 06-Dec-05                                       Time: 19:02:23
------------------------------ XFMail ------------------------------
#
On 12/6/05, Ted Harding <Ted.Harding at nessie.mcc.ac.uk> wrote:
I'm curious: exactly what purpose does this 'compactness' serve? The
file size doesn't decrease, since you are replacing newlines by
semicolons. It does not improve readability. So why do it at all?

-Deepayan
#
On 06-Dec-05 Deepayan Sarkar wrote:
You are taking a more abstract and logical a view than I do!

a) It is more compact in the sense that the same anount of code
   takes fewer lines.

b) My "editing" window is typically about 56 lines tall. Once
   I have got the code working as I want, I can "compact" it onto
   fewer lines, thereby leaving more space for further code all
   of which will be visible in the same screen space.

c) Since the "compacted" code is already OK, I don't need to
   be able to read it so readily -- it is enough that I can
   see it when I need to refer to it.

It is all a matter of layout, perception and psychology: by experience
I have found that this way of working improves my accuracy and speed,
and my overview of the problem (and hence my ability to see solutions).

This may or may not be valid for anyone else; but as far as I'm
concerned it is (along with the "J" trick when using vim) a cogent
(if personal) reason for putting ";" at the ends of commands.
Which was the original point.

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 06-Dec-05                                       Time: 21:01:30
------------------------------ XFMail ------------------------------
#
On Tue, 06-Dec-2005 at 01:00PM +0000, Adaikalavan Ramasamy wrote:
|> Yes, it drives me mad too when people use "=" instead of "<-" for
|> assignment and suppress spaces in an naive attempt for saving space. 
|> 
|> As an example compare 
|> 	
|> 	o=fn(x=1,y=10,z=1)
|> 
|> with
|> 
|> 	o <- fn( x=1, y=10, z=1 )


Or better still:

o <- fn(x = 1, y = 10, z = 1)

The effect is more marked when the arguments are whole words rather
than the single letter names in this example.

Compare

o <- fn(xena = log, yacht = 10625, zebra = "green")

with 

o <- fn( xena=log, yacht=10625, zebra="green" )




|> 
|> Regards, Adai
|> 
|> 
|>
|> On Tue, 2005-12-06 at 13:43 +0100, Martin Maechler wrote:
|> > >>>>> "vincent" == vincent  <vincent at 7d4.com>
|> > >>>>>     on Tue, 06 Dec 2005 11:09:36 +0100 writes:
|> > 
|> >     vincent> shanmuha boopathy a ??crit :
|> >     >> a<-function(a,b,c,d)
|> >     >> {
|> >     >> k=a+b
|> >     >> l=c+d
|> >     >> m=k+l
|> >     >> }
|> >     >> 
|> >     >> in this example the function will return only the value of "m"
|> >     >> ...But I like to extract the values of "l" & "k" also.........
|> >     >> which command to use for storing or for extracting those intermediate value.......
|> > 
|> >     vincent> may I suggest, inside your function
|> > 
|> >     vincent> res = c(k, l, m);
|> >     vincent> return(res);
|> > 
|> > please, please,  these trailing ";"  are  *so* ugly.
|> > This is GNU S, not C (or matlab) !
|> > 
|> > {and I have another chain of argments why   "<-" is so more
|> > expressive than "="  but I'll be happy already if you could
|> > drop these ugly empty statements at the end of your lines...
|> > 
|> >     vincent> # also ... read some intro docs !
|> > 
|> > ______________________________________________
|> > R-help at stat.math.ethz.ch mailing list
|> > https://stat.ethz.ch/mailman/listinfo/r-help
|> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
|> >
|> 
|> ______________________________________________
|> R-help at stat.math.ethz.ch mailing list
|> https://stat.ethz.ch/mailman/listinfo/r-help
|> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
#
Hello,

About the "R style war": as soon as you write code for yourself, you can 
do what you want, of course. If your code is shared (for instance, code 
in packages submitted to CRAN), it makes sense to render it so that it 
is easier to read *by a majority of people*. The only way to make sure 
it is easy to read  by most people is to follow strictly the style 
proposed by the R core team (even if you don't like it)!

So, if you write both code for yourself and for public release, and/or 
if you think that your code will become public one day, it is better to 
follow R core team style from the beginning. Similarly, you should never 
use T/F instead of TRUE/FALSE in code that will go to a library ('cause 
you'll have to change it anyway)... So, why not to take the habit to 
write TRUE/FALSE all the time in your code?

The conclusion: it is far better to strictly follow R core team style 
for *all* code (your own plus the one your share), because it is easier 
to stick on one style... and, don't worry, you will gradually become 
accustomished to the aspects you don't like in that style.

So, who said there is an "R style war"? There is one set of rules to 
follow. Point. Don't you have better things to do that to discuss if one 
needs a space here (a <- 1), or not (a<-1) ?

Best,

Philippe Grosjean
Patrick Connolly wrote:
#
On Tue, 06-Dec-2005 at 04:21PM +0000, Patrick Burns wrote:
|> I don't put in extraneous ';' because I maybe get a
|> blister on my little finger.
|> 
|> I suspect that those who find the semi-colons ugly in
|> R do not find them ugly in C. 

Nor in perl, mysql or php.  I quite like how R is rather different
from those other languages.  Somehow that very different look helps to
change (me at least) into the appropriate type of thinking.  It's
somewhat akin to the difference between English and German use of
capital letters -- each system makes perfect sense in its own context
and are beter not mixed.
#
Philippe Grosjean a ??crit :
I quite agree with your message, but as far as I understood
today, there is no "official R style" chart,
(official = from the R core dev team).

http://www.maths.lth.se/help/R/RCC/ is interesting (thanks to
the author), but is not from the R-developers.

The best I found is
http://cran.r-project.org/doc/manuals/R-exts.html#R-coding-standards
but it says very few about a recommended style
(except for indentations).

Apologies if I miss the definite *official* document concerning
"one set of rules to follow", and thanks to give us the adress
of this document.
#
Oh Patrick, surely German Capitalization is better!

:)

Peter
Patrick Connolly wrote: