[R] R coding style (was R is GNU S, not C....)

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Dec 8 08:21:01 CET 2005


Please use an informative subject line.

On Wed, 7 Dec 2005 vincent at 7d4.com wrote:

> Philippe Grosjean a écrit :
>
>> So, who said there is an "R style war"? There is one set
>> of rules to follow. Point.
>
> 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.

Yes, those are the 'Bengtsson Coding Conventions'.  (He says 'our', but it 
appears to be a royal 'we'.)  Some contradict the primary documentation 
(e.g. for S3 generics where dispatch can usefully occur on particular 
named arguments), and the author seems unaware that underline is allowed 
in syntactic names (and is preferred to capitalization schemes by many).

> 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.

I had already posted this in this thread: it is in sections 3.1 and 
Appendix B of `Writing R Extensions'.  (You seem unaware that this is a 
manual that ships with every copy of R, as you give a URL on CRAN for 
part of it.)

The style R itself uses to deparse code is canonical.  As in

> options(keep.source=FALSE)
> test <- function(){
+ abc=T;
+ 7+8;
+ }
> test
function ()
{
     abc = T
     7 + 8
}

Voila, those distracting semicolons have disappeared! (R has removed the 
pointless empty statement which was specified at the end of the lines, 
between the semicolon and newline.)

Note how spaces are used to improve clarity.

Note that the use of T has not, as using T as a variable is legitimate R. 
The manual in 2.2.0 (but not 2.2.1 beta) says <- will be used for 
assignment, and indeed the equivalent _ was converted to <- whereas = is a 
different token (and postdates that manual section).  But it does say <- 
is preferred.

To quote:

   This tidied version is much easier to read, not least by other users
   who are used to the standard format.

and that is the main point, reinforced by Philippe's message.  If you are 
trying to communicate, make life easier for your readers.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list