小编use*_*814的帖子

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"
Run Code Online (Sandbox Code Playgroud)

然后我想'a'从定义中删除插槽'foo'

setClass(Class = 'foo', slots = c(
  b = 'character'
))
slotNames(x) # slot 'a' automatically removed!! wow!!!
Run Code Online (Sandbox Code Playgroud)

我看到 R 会自动处理我的对象x'a'移除插槽。好的!但是等等,对象的大小x并没有减少。

format(object.size(x), units = 'auto') # …
Run Code Online (Sandbox Code Playgroud)

r slot s4

6
推荐指数
1
解决办法
178
查看次数

标签 统计

r ×1

s4 ×1

slot ×1