[R] Google's R Style Guide (has become S3 vs S4, in part)

spencerg spencer.graves at prodsyse.com
Tue Sep 1 17:13:42 CEST 2009


Bryan Hanson wrote:
> Looks like the discussion is no longer about R Style, but S3 vs S4?
>
> To that end, I asked more or less the same question a few weeks ago, arising
> from the much the same motivations.  The discussion was helpful, here's the
> link:  
>
> http://www.nabble.com/Need-Advice%3A-Considering-Converting-a-Package-from-S
> 3-to-S4-tc24901482.html#a24904049
>
> For what it's worth, I decided, but with some ambivalence, to stay with S3
> for now and possibly move to S4 later.  In the spirit of S4, I did write a
> function that is nearly the equivalent of validObject for my S3 object of
> interest.
>
> Overall, it looked like I would have to spend a lot of time moving to S4,
> while staying with S3 would allow me to get the project done and get results
> going much faster (see Frank Harrell's comment in the thread above).
>
> As a concrete example (concrete for us non-programmers, non-statisticians),
> I recently decided that I wanted to add a descriptive piece of text to a
> number of my plots, and it made sense to include the text with the object.
> So I just added a list element to the existing S3 object, e.g.
> Myobject$descrip  No further work was necessary, I could use it right away.
> If instead, if I had made Myobject an S4 object, then I would have to go
> back, redefine the object, update validObject, and possibly write some new
> accessor and definitely constructor functions.  At least, that's how I
> understand the way one uses S4 classes.
>   
      It doesn't stop there:  If you keep the same name for your 
redefined S4 class, I don't know what happens when you try to access 
stored objects of that class created before the change, but it might not 
be pretty.  If you give your redefined S4 class a different name, then 
you have a lot more code to change before you can use the redefined 
class like you want. 


      By contrast, with S3, if you have any code that tests the number 
of components in a list, that will have to be changed. 


      Spencer
> Back to trying to get something done!  Bryan
> *************
> Bryan Hanson
> Professor of Chemistry & Biochemistry
> DePauw University, Greencastle IN USA
>
>
>
>
>
> On 9/1/09 6:16 AM, "Duncan Murdoch" <murdoch at stats.uwo.ca> wrote:
>
>   
>> Corrado wrote:
>>     
>>> Thanks Duncan, Spencer,
>>>
>>> To clarify, the situation is:
>>>
>>> 1) I have no reasons to choose S3 on S4 or vice versa, or any other coding
>>> convention
>>> 2) Our group has not done any OO developing in R and I would be the first, so
>>> I 
>>> can set up the standards
>>> 3) I am starting from scratch with a new package, so I do not have any code I
>>> need to re-use.
>>> 4) I am an R OO newbie, so whatever I can learn from the beginning what is
>>> better and good for me.
>>>
>>> So the questions would be two:
>>>
>>> 1) What coding style guide should we / I follow? Is the google style guide
>>> good, or is there something better / more prescriptive which makes our
>>> research group life easier?
>>>   
>>>       
>> I don't think I can answer that.  I'd recommend planning to spend some
>> serious time on the decision, and then go by your personal impression.
>> S4 is definitely harder to learn but richer, so don't make the decision
>> too quickly.  Take a look at John Chamber's new book, try small projects
>> in each style, etc.
>>
>>     
>>> 2) What class type should I use? From what you two say, I should use S3
>>> because is easier to use .... what are the disadvantages? Is there an
>>> advantages / disadvantages table for S3 and S4 classes?
>>>   
>>>       
>> S3 is much more limited than S4.  It dispatches on just one argument, S4
>> can dispatch on several.  S3 allows you to declare things to be of a
>> certain class with no checks that anything will actually work; S4 makes
>> it easier to be sure that if you say something is of a certain class, it
>> really is.  S4 hides more under the hood: if you understand how regular
>> R functions work, learning S3 is easy, but there's still a lot to learn
>> before you'll be able to use S4 properly.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> 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.
>>     
>
> ______________________________________________
> 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.
>
>   


-- 
Spencer Graves, PE, PhD
President and Chief Operating Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567




More information about the R-help mailing list