扩展 R S4 对象以拥有新的插槽并保持原始对象以相同的方式工作 [英] Extend an R S4 object to have new slots and keep the original object working the same way

查看:50
本文介绍了扩展 R S4 对象以拥有新的插槽并保持原始对象以相同的方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DESeqResults"类的 S4 对象.本质上,我希望能够向该对象添加新信息.理想情况下,我只想向其中添加几个插槽,以便可以存储以下内容:

I have an S4 object of class "DESeqResults". Essentially, I want to be able to add new information to this object. Ideally, I'd just like to add a few slots to it so I can store things like:

myDESet@new_slot = 5 

我开始认为我必须创建一个继承DESeqResults"的子类,虽然我还不确定如何在 R 中做到这一点,但我更关心如何保持原始对象的数据完整.

I'm starting to think I'll have to make a subclass that inherits "DESeqResults" and while I'm not exactly sure how to do that as of yet in R, I am more concerned about how to keep the data from the original object in tact.

本质上,一个库正在制作和使用这个DESeqResults"类对象,在它被创建并拥有一些数据之后,它将被用于很多功能.在创建其中之一之后,我只想向对象添加一些新信息.如果我创建一个扩展这个类并有额外插槽的类,我如何将所有现有数据从类的原始实例转移到子类的新实例中?

Essentially, a library is making and using this "DESeqResults" class object, and after it is created and has some data, it will be used for a lot of functionality. After I create one of these then, I just want to add some new information to the object. If I make a class that extends this class and has extra slots, how could I transfer all the existing data from the original instance of the class into a new instance of the subclass?

实现我在这里尝试做的事情的最佳方式是什么?是否可以在实例化任何对象之前修改原始类,以便在创建它们时具有我需要的额外插槽?或者有没有其他方法可以实现这一目标?

What would be the best way to go about achieving what I'm trying to do here? Is it possible to modify the original class before any objects are instantiated so that when they are created they have the extra slots I need? Or is there some other way to achieve this?

非常感谢!

推荐答案

通常的做法是定义一个新的父类的子类:

The usual way to do this is to define a new subclass of the parent class:

setClass(
  "myDESRclass",
  contains="DESeqResults",
  slots=c(new_slot="numeric")
) -> myDESRclass

然后您可以使用 as 将对象转换为您的类:

Then you can use as to convert objects to your class:

## x is some DESeqResults object
x <- as(x,"myDESRclass")
x@new_slot <- 5

在大多数情况下,您必须调用 setAs 或类似方法,但由于 DESeqResults 是超类,as 方法由 R 预先定义,并且按预期工作.

In most cases you have to make a call to setAs or similar but because DESeqResults is a superclass the as method is pre-defined by R, and works as intended.

如果您不想采用这种方法,有两种选择,但它们不太安全:

If you don't want to take this approach, there are two alternatives but they are less safe:

1) 使用 S3 而不是 S4.听起来您并不拥有" DESeqResults 类,因此这可能很困难,但是 myDESet$new_slot <- 5 不会出错.

1) Use S3 instead of S4. It sounds like you don't "own" the DESeqResults class so that could be difficult, but myDESet$new_slot <- 5 will not be an error.

2) 插槽作为属性实现,因此您可以使用 attr(myDESet,"new_slot") <- 5 设置一个.尽管如此,这个修改过的对象仍然无法通过任何有效性检查,因此这可能非常不稳定.

2) Slots are implemented as attributes, so you can set one with attr(myDESet,"new_slot") <- 5. This modified object will still fail any validity check though, so this can be quite unstable.

这篇关于扩展 R S4 对象以拥有新的插槽并保持原始对象以相同的方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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