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

查看:13
本文介绍了在 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 实现也可以使用经典列表轻松完成,无需像定义标准泛型、方法、构造函数、初始化器等那样麻烦.

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.

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天全站免登陆