在R中创建类:S3,S4,R5(RC)或R6? [英] Creating Classes in R: S3, S4, R5 (RC), or R6?

查看:180
本文介绍了在R中创建类:S3,S4,R5(RC)或R6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点在公开一个问题,以寻找在R中创建新类的优点或缺点.据我所知,在R,S3,S4,R5中创建类时基本上使用了四种不同的范例(或RC)和R6.

I'm kind of posting an open question to look for advantages or disadvantages to creating new classes in R. From what I can tell, there's essentially four different paradigms that are used when creating classes in R, S3, S4, R5 (or RC), and R6.

S3是R的大多数核心库使用的东西,坚持使用泛型方法分派的简单,结构轻巧的模式似乎有一些优点.我想避免因为其他我不太清楚的原因而使用它,例如封装,方法的定义等.例如,构造类时似乎很麻烦,因为泛型方法是在类之外定义的.阶级,以及那种性质的东西.

S3 is what most of the core libraries of R use, and there seems to be some merit to sticking with the simple, lightly structured pattern of using generic method dispatching. I'd like to avoid using this for other reasons that aren't quite clear to me, such as encapsulation, definitions of methods, etc. For example, it seems rather cumbersome when structuring a class, because generic methods are defined outside of the class, and things of that nature.

S4似乎没有更好的表现,但是它确实包含了穷人的类型安全性概念,以使可能出现的明显错误变得更加明显.但是,我仍然觉得S4类很难维护,因为我不确定这些类所涉及的诸如封装之类的事情.似乎让我感到困惑的另一件事是,几乎没有命名空间的概念.

S4 doesn't seem to be any better, but it does have a poor-man's notion of type safety involved with it, to make obvious mistakes that might arise more apparent. However, I still feel like the S4 classes are hard to maintain in the sense that I'm unsure about things such as encapsulation and such that are involved with these classes. Another thing that seems to be confusing me is that there is little to no notion of namespacing.

R5似乎有点像我惯用的那样,方法的定义绑定到类上,而不是分派函数.在这里,在按照我习惯的类来组织对象方面,还有更多的想法可以发挥作用.一个可能的缺点是R5也是在S4的基础上构建的.

R5 seems to be a little more akin to what I'm used to, where the definitions of methods are bound to classes, rather than dispatching functions. Here, there's a little bit more thought that comes into play with organizing an object in terms of a class that I would be used to. One possible disadvantage is that R5 is also built off of S4.

R6似乎是个人对R5的重写,它向组合中添加了更多的OOP功能,例如私有和公共功能和属性,但是我几乎找不到其他对这些类的支持,因为关于它们的信息似乎通过Google搜索稀疏.

R6 seems to be a rewrite of R5 by an individual that adds more OOP features to the mix, such as private and public functions and properties, but I can hardly find any support for these classes otherwise, as information about them seems to be sparse through Google searching.

您可以说,我在R中的OO概念上苦苦挣扎,而且似乎无法弄清通常与OOP相关的以下方面:

As you can tell, I'm struggling with the OO concepts in R and I can't seem to figure out the following facets that are normally associated with OOP:

  1. 类型安全性/类型
  2. 方法/对象绑定,封装,成员变量等,
  3. 命名和组织代码
  4. 继承的版本.

我想知道是否有人可以提供一个答案,以描述R社区中首选的类系统以及如何最好地考虑何时使用类.

I'm wondering if someone can provide an answer that can describe what the preferred class system is in the R community, and how to best think about when to use classes.

推荐答案

似乎您已经知道各种OOP类型的一些定义和用法.我将在何时使用哪个方面给出我的意见.

It seems you are already aware of some of the definitions and uses for the various OOP types. I will give my opinion on when it is appropriate to use which.

  1. 在满足以下两个条件的情况下使用S3类:(a)您的对象是静态的并且不能自我修改,并且(b)您不关心多参数方法签名,即您的方法仅在其第一个参数(对象的S3类)上分派.另外,当您可以忍受这些限制并希望使许多运算符重载时,S3类是一个很好的解决方案.

  1. Use S3 classes for situations where both of the following apply: (a) your object is static and not self-modifying, and (b) you do not care about multi-argument method signatures, i.e., your method dispatches purely on its first argument, the S3 class of the object. Additionally, S3 classes are a good solution when you can live with these restrictions and want to overload many operators.

如果您的对象是静态的并且不能自我修改,请使用S4类,但是您需要考虑多参数方法签名.根据我的经验,尽管S4 OOP在某种程度上可以保证"类型的安全性,但它总是比其价值更麻烦.

Use S4 classes if your object is static and not self-modifying, but you care about multi-argument method signatures. From my experience, S4 OOP has always been more hassle than it is worth, although it "guarantees" type safety to some extent.

如果对象是自修改的,则使用引用类.否则,您将必须定义许多替换方法(例如some_method<-,用语法some_method(obj) <- value调用).由于R每次都会创建对象的完整副本,因此这很麻烦且计算速度很慢. R6是一个很好的替代品,尽管我没有发现它对我的目的是必需的.

Use reference classes if your object is self-modifying. Otherwise, you will have to define many replace methods (e.g., some_method<-, which is called with the syntax some_method(obj) <- value). This is awkward and computationally slow, since R will be creating a full copy of the object each time. R6 is a good substitute, although I have not found it necessary for my purposes.

R的大多数新手都觉得它很困惑;这么多OOP实现的原因是因为没有达成共识.

Most people new to R think it is confused; that the reason there are so many OOP implementations is because there was no consensus.

这是不正确的.

由于其统计性质,R中的大多数异构结构(即应该是客观的东西)最终都是统计算法的结果:lm,glmnet,gbm等对象.通常只需捆绑这些信息并提供用于汇总它的预期接口即可:printsummary等.

Due to its statistical nature, most heterogeneous structures in R (i.e, things that should be objecty) end up being the result of a statistical algorithm: an lm, glmnet, gbm, etc. object. It usually suffices to bundle this information and provide the expected interfaces for summarizing it: print, summary, etc.

由于其作为统计场所的传统,这使用户不必考虑诸如继承和分配/取消分配之类的更高级的概念,并为更多的贡献者开放了竞争环境.这意味着用R创建复杂项目(例如Web服务器,文本解析器,图形界面等)比使用诸如Ruby之类的典型对象驱动语言要稍微麻烦一些,但是缺少统一的OOP类型是通过易用性达到平衡.

Owing to its legacy as a statistical playground, this frees the user from having to think about more advanced concepts like inheritance and allocation / de-allocation, and opens the playing field to more contributors. This means that it is slightly more annoying to create complex projects (e.g., web servers, text parsers, graphical interfaces, etc.) in R than in a typical object-driven language like Ruby, but the lack of a uniform OOP-type is balanced by ease of use.

考虑的最后一种方法是,不同的方法就像物质中的相变:固体,气体,液体.与其统一对待所有异类结构(即类似OOP的事物),有些结构更自然地属于一种结构而不是另一种结构.如果我将一个简单的列表包装在S3类中以便通过重载的print方法很好地显示,则为此目的而建立一个整个引用类将是很愚蠢的.

One final way to think about it is that the different approaches are like phase transitions in matter: solid, gas, liquid. Rather than treating all heterogeneous structures (i.e., OOP-like things) uniformly, some fall more naturally under one structure than another. If I am wrapping a simple list in an S3 class to display nicely with an overloaded print method, it would be rather silly to set up a whole reference class for this purpose.

这篇关于在R中创建类:S3,S4,R5(RC)或R6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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