[R-wiki] Be careful of syntax!

Philippe Grosjean phgrosjean at sciviews.org
Wed Feb 1 14:57:23 CET 2006


..............................................<°}))><........
  ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
  ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
  ) ) ) ) )   Mons-Hainaut University, Pentagone (3D08)
( ( ( ( (    Academie Universitaire Wallonie-Bruxelles
  ) ) ) ) )   8, av du Champ de Mars, 7000 Mons, Belgium
( ( ( ( (
  ) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
( ( ( ( (    email: Philippe.Grosjean at umh.ac.be
  ) ) ) ) )
( ( ( ( (    web:   http://www.umh.ac.be/~econum
  ) ) ) ) )          http://www.sciviews.org
( ( ( ( (
..............................................................

Peter Dalgaard wrote:
> Philippe Grosjean <phgrosjean at sciviews.org> writes:
> 
> 
>>- Try to format R code as the implicit "R-Core Team formatting", in 
>>particular:
>>   + Forget about T/F and always use TRUE/FALSE
>>   + Forget about '=' for assignation ands always use '->'
>>   + Use spaces for clearer code in math operations, after commas, 
>>between the equal sign in named arguments, for instance:
>>
>>f <- function(x, y, nam.rm = TRUE) {
>>     if (na.rm) {
>>         x <- na.omit(x)
>>         y <- na.omit(y)
>>     }
>>     x + log(y)^2 - y
>>}
>>
>>   + Indent (use four spaces for each indentation instead of tabs, to 
>>make sure it is correct)
>>   + Bracket indentation should be as the example here above.
>>   + Use named arguments as much as possible, because it is more explicit)
>>   + Use meaningful variable names
> 
> 
> And of course, make sure that the code actually works (the above
> does not in two different ways...)

Oooops! Thank you Peter. One should read something like:

f <- function(x, y, na.rm = TRUE) {
     dat <- data.frame(x, y)
     if (na.rm)
         dat <- na.omit(dat)
     return(dat$x + log(dat$y)^2 - dat$y)
}

(note that the function is not doeing something useful... it is just to 
illustrate code formatting!)

Philippe Grosjean



More information about the R-sig-wiki mailing list