R:如何真正从S4对象中移除S4插槽(附有解决方案!)
假设我定义了一个'foo'带有两个插槽'a'和的 S4 类'b',并定义了一个x类的对象'foo',
setClass(Class = 'foo', slots = c(
a = 'numeric',
b = 'character'
))
x <- new('foo', a = rnorm(1e3L), b = rep('A', times = 1e3L))
format(object.size(x), units = 'auto') # "16.5 Kb"
然后我想'a'从定义中删除插槽'foo'
setClass(Class = 'foo', slots = c(
b = 'character'
))
slotNames(x) # slot 'a' automatically removed!! wow!!!
我看到 R 会自动处理我的对象x并'a'移除插槽。好的!但是等等,对象的大小x并没有减少。
format(object.size(x), units = 'auto') # still "16.5 Kb"
format(object.size(new(Class = 'foo', x)), units = 'auto') # still "16.5 Kb"
对.. 不知何故'a'仍然存在,但我无法对它做任何事情
head(x@a) # `'a'` is still there
rm(x@a) # error
x@a <- NULL # error
所以问题:我如何才能真正去除插槽'a'从x和有它的尺寸缩小(这是我的主要关注的)?
我最深切的感谢所有的答案!
以下解决方案的灵感来自 dww
trimS4slot <- function(x) {
nm0 <- names(attributes(x))
nm1 <- names(getClassDef(class(x))@slots) # ?methods::.slotNames
if (any(id <- is.na(match(nm0, table = c(nm1, 'class'))))) attributes(x)[nm0[id]] <- NULL # ?base::setdiff
return(x)
}
format(object.size(y1 <- trimS4slot(x)), units = 'auto') # "8.5 Kb"
以下解决方案的灵感来自 Robert Hijmans
setClass('foo1', contains = 'foo')
format(object.size(y2 <- as(x, 'foo1')), units = 'auto') # "8.5 Kb"
method::as 可能会做一些全面的检查,所以速度很慢
library(microbenchmark)
microbenchmark(trimS4slot(x), as(x, 'foo1')) # ?methods::as 10 times slower