什么时候在R编程中使用S4方法值得 [英] When does it pay off to use S4 methods in R programming

查看:71
本文介绍了什么时候在R编程中使用S4方法值得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在专业环境下用R编程,也为客户或同事编写程序包.这里的一些程序员具有Java背景,并坚持使用S4方法以面向对象的方式进行所有操作.另一方面,我的经验是,在尝试使代码执行您想要执行的操作时,S4实现通常会执行得更糟,并且引起更多的麻烦.

I program regularly in R in a professional context, and I write packages for clients or co-workers as well. Some of the programmers here have a Java background and insist on doing everything the object-oriented way, using S4 methods. My experience on the other hand is that S4 implementations often perform worse and cause a lot more headache when trying to get the code do what you want it to do.

我绝对同意在某些情况下,您必须能够以受控的方式构造复杂的对象或追加现有对象.但是在大多数情况下,也可以使用经典列表轻松实现S4实现,而无需像定义standardGeneric,方法,构造函数,初始化程序之类的麻烦.

I definitely agree that in some cases, you have to be able to construct complex objects or append existing objects in a controlled manner. But most of the time, S4 implementations can easily be done using classic lists as well, without all the hassle like defining standardGeneric, methods, constructors, initializers and the likes.

您何时考虑为R编写S4实现?

When do you consider writing S4 implementations for R?

为清楚起见,我确实很欣赏R的回答和有关OO的一般讨论.OOP可以在R中以多种方式完成,但是我的问题实际上是专门针对使用S4方法的附加价值.

EDIT : For clarity, I do appreciate the answers and the discussion about OO in general in R. OOP can be done in numerous ways in R, but my question is really aimed at the added value of using S4 methods specifically.

推荐答案

我假设这并不直接适用于您,但是如果您正在为Bioconductor开发软件包,则有一种使用S4的动机,因为他们积极鼓励使用S4.使用并拥有了十年的大部分时间-因此所有核心程序包都大量使用了S4.

I'm assuming this doesn't directly apply to you, but if you're developing packages for Bioconductor there's an incentive to use S4 as they actively encourage it's use and have for the better part of a decade now - so all of the core packages make heavy use of S4.

我发现所有额外的开销都很痛苦-setGeneric,setMethod,处理NAMESPACE等.话虽如此,我发现它所强加的结构,潜在的可扩展性以及其他类似的东西是值得的.与所有内容一样,也要进行权衡.我认为这样可以使工作更清洁-我不喜欢S3方法是如何简单地通过命名约定(foo.class)伪装的.话虽这么说,除非我被告知这样做,否则我倾向于避免在自己的代码中大量使用S4.

I find all of the extra overhead to be a pain - the setGeneric, setMethod, dealing with NAMESPACE, etc. That being said, I find that the structure that it imposes, potential for extensibility and other such things can be worth it. As with everything, there are tradeoffs involved. I think it can be a lot cleaner - I dislike how S3 methods are simply disguised by naming convention (foo.class). All that being said, I tend to avoid using S4 heavily in my own code unless I'm being told to do so.

这篇关于什么时候在R编程中使用S4方法值得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆