如何在 R 中为自定义 S3 类编写 ac() 函数

发布于 2024-10-04 08:55:06 字数 745 浏览 5 评论 0原文

我正在 R 中编写一个 S3 类,它只是一个附加了一些属性的整数。如果 x1 和 x2 是此类的对象(称为“myclass”),那么我希望 c(x1, x2) 返回 myclass 对象的向量,其中原始类定义和属性完好无损。但是,c() 的记录行为是删除属性,因此我似乎需要编写自己的 c.myclass() 方法。我的问题是,我该怎么做?

问题的一个例子:

myclass <- function(x, n) structure(x, class="myclass", n=n)
x1 <- myclass(1, 5)
x2 <- myclass(2, 6)
c(x1, x2)
[1] 1 2

这里的结果只是一个数字类项目的向量,原始的 n 属性消失了。

查看各种包的代码,我有时会看到如下代码,其中我们需要保留类属性,但不需要保留其他内容:

c.myclass <- function(..., recursive = F) {
    structure(c(unlist(lapply(list(...), unclass))), class="myclass")
}

不幸的是,我也无法使其工作。调用 c.myclass(x1, x2) 的结果是一个向量,其中向量本身具有类“myclass”,但向量中的每个项目具有类 numeric;我真的希望向量中的每个项目都有类“myclass”。在实践中,我还需要升级此方法以保留其他属性(例如 myclass 中的属性“n”)。

I'm writing an S3 class in R that is just an integer with some attributes attached to it. If x1 and x2 are objects of this class (call it "myclass"), then I would like c(x1, x2) to return a vector of myclass objects with the original class definition and attributes intact. However, the documented behavior of c() is to remove attributes, so it would seem that I need to write my own c.myclass() method. My question is, how can I do this?

An example of the problem:

myclass <- function(x, n) structure(x, class="myclass", n=n)
x1 <- myclass(1, 5)
x2 <- myclass(2, 6)
c(x1, x2)
[1] 1 2

Here the result is just a vector of items of class numeric, and the original n attribute is gone.

Looking at the code for various packages, I sometimes see code like the following, in which we need to preserve the class attribute but nothing else:

c.myclass <- function(..., recursive = F) {
    structure(c(unlist(lapply(list(...), unclass))), class="myclass")
}

Unfortunately I also cannot get this to work. The result of calling c.myclass(x1, x2) is a vector where the vector itself has class "myclass" but where each item in the vector has class numeric; I really want each item in the vector to have class "myclass". In practice I will also need to upgrade this method to preserve other attributes as well (like the attribute "n" in myclass).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她如夕阳 2024-10-11 08:55:06

这是一个示例,(我认为)通过 c[ 的特定方法实现您想要的功能:

c.myclass <- function(..., recursive = FALSE) {
    dots <- list(...)
    ns <- sapply(dots, attr, which = "n")
    classes <- rep("myclass", length(dots))
    res <- structure(unlist(dots, recursive = FALSE), class = classes)
    attr(res, "n") <- ns
    res
}

`[.myclass` <- function (x, i) {
    y <- unclass(x)[i]
    ns <- attr(x, "n")[i]
    class(y) <- "myclass"
    attr(y, "n") <- ns
    y
}


myclass <- function(x, n) structure(x, class = "myclass", n = n)
x1 <- myclass(1, 5)
x2 <- myclass(2, 6)
c(x1, x2)
c(x1, x2)[2]

但这只是一个谎言,因为我们必须管理处理设置和额外属性的子集以保存n。这实际上只是一个带有记录n 属性的数值向量。

使用所有向量的通用属性(列表)可能会更自然。这有点复杂,也许上面的内容对于您的情况来说就足够了?

Here is an example that does (I think) what you want via specific methods for c and [:

c.myclass <- function(..., recursive = FALSE) {
    dots <- list(...)
    ns <- sapply(dots, attr, which = "n")
    classes <- rep("myclass", length(dots))
    res <- structure(unlist(dots, recursive = FALSE), class = classes)
    attr(res, "n") <- ns
    res
}

`[.myclass` <- function (x, i) {
    y <- unclass(x)[i]
    ns <- attr(x, "n")[i]
    class(y) <- "myclass"
    attr(y, "n") <- ns
    y
}


myclass <- function(x, n) structure(x, class = "myclass", n = n)
x1 <- myclass(1, 5)
x2 <- myclass(2, 6)
c(x1, x2)
c(x1, x2)[2]

But that is a fudge in that we have to manage handling the setting and subsetting of extra attributes to hold the n. This is really just a numeric vector with an attribute for recording n.

It might be more natural to work with the generic of all vectors, a list. Bit that is more involved and maybe the above is sufficient in your case?

太阳男子 2024-10-11 08:55:06

这确实有效,但我假设您得出结论,每个向量元素都具有数字类,因为您正在执行以下操作:

foo <- c(x1, x2)
class(foo[1])
class(foo[2])

如果是这种情况并且您希望提取的元素保留 myclass 属性,则需要编写一个子集方法 "[.myclass" 来保留属性。

This does work, but I assume you conclude that each vector element has class numeric because you're doing something like this:

foo <- c(x1, x2)
class(foo[1])
class(foo[2])

If that's the case and you want extracted elements to retain the myclass attribute, you need to write a subset method "[.myclass" to retain the attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文