R S4对象深度复制
我附上下面的代码,以便更清楚;我有一个称为grid的S4对象矩阵,每个对象都有一个状态和一个hashmap,如类定义所示R S4对象深度复制,r,clone,s4,R,Clone,S4,我附上下面的代码,以便更清楚;我有一个称为grid的S4对象矩阵,每个对象都有一个状态和一个hashmap,如类定义所示 cell <- setClass( # Set the name for the class "cell", # Define the slots slots = c( state = "numeric", code = "hash" ), # Set the default values for the slots. p
cell <- setClass(
# Set the name for the class
"cell",
# Define the slots
slots = c(
state = "numeric",
code = "hash"
),
# Set the default values for the slots.
prototype=list(
state = 1
)
)
setGeneric(name="copy_cell",
def= function(x, y){
standardGeneric("copy_cell")
})
setMethod(f = "copy_cell", signature = "cell",
definition = function(x, y) {
x@state = y@state
x@code = copy(y@code)
})
grid <- matrix( sapply(1:(20*20), function(x){
new("cell",
state = ifelse(runif(1)<= 0.5, 3, 1),
code = hash())
}), 20, 20)
但我很难实现将函数应用于矩阵所有单元的方法。下面,我首先将网格复制到nextgrid中,以避免重新初始化另一个完整的对象矩阵,然后使用sapply
nextgrid = grid
sapply(grid[,], function(x) copy_cell(nextgrid[,], x))
我得到错误:在(函数(类,fdef,mtable)中出错:找不到签名“matrix”的函数“copy_cell”的继承方法。
我理解这一点,因为我似乎在向函数传递矩阵,而不是单个单元格……但我不知道如何正确地重新编写它
我试着用R6对象重写我的类,这些对象有一个很好的克隆/深度克隆方法,但是我真的很难为其他操作切割矩阵,所以我有点卡住了
想法?我建议使用R5引用类中的复制构造函数。请注意,“hash”属性在引用类中非常特殊,因为hash映射的函数名
copy
与复制构造函数的名称一致。我们可以使用eval.parent(quote(copy))
作为检索复制构造函数的解决方法
进一步注意,复制空哈希映射(即,copy(hash())
)失败。这似乎是哈希库的一个错误(或功能?)。因此,我还向网格项的哈希映射添加了一个对象
library(hash)
cell <- setRefClass(
# Set the name for the class
"cell",
# Define the slots
fields = list(
state = "numeric",
code = "hash"
),
methods = list(
# Default constructor
initialize = function(state, code) {
.self$state = state
.self$code = code
},
# Copy constructor
copy = function(shallow = FALSE) {
# Get the copy-function for hash from the parent environment
cp <- eval.parent(quote(copy))
# do the copy
cell(.self$state, cp(code))
}
)
)
# Create cell and copy it
cell1 = cell(ifelse(runif(1)<= 0.5, 3, 1), hash("a",1))
cell2 = cell1$copy()
# Let's convince ourselves that we did a deep copy
cell1$code[["a"]]=5
cell2
# Create grid and copy it
grid <- matrix( sapply(1:(20*20), function(x){
cell(ifelse(runif(1)<= 0.5, 3, 1), hash("a",1))
}), 20, 20)
nextgrid = apply(grid, c(1,2), function(x) x[[1]]$copy())
# Let's again convince ourselves that we did a deep copy
grid[[1,1]]$code["a"] = 5
nextgrid[[1,1]]
库(散列)
单元格正确。如何使用当前设置创建网格?如果我想确保第一行和最后一行以及第一列和最后一列中的所有单元格的状态都为1,是否有一种有效的方法?只需向下滚动查看网格是如何创建的:)您可以使用grid[20,]
library(hash)
cell <- setRefClass(
# Set the name for the class
"cell",
# Define the slots
fields = list(
state = "numeric",
code = "hash"
),
methods = list(
# Default constructor
initialize = function(state, code) {
.self$state = state
.self$code = code
},
# Copy constructor
copy = function(shallow = FALSE) {
# Get the copy-function for hash from the parent environment
cp <- eval.parent(quote(copy))
# do the copy
cell(.self$state, cp(code))
}
)
)
# Create cell and copy it
cell1 = cell(ifelse(runif(1)<= 0.5, 3, 1), hash("a",1))
cell2 = cell1$copy()
# Let's convince ourselves that we did a deep copy
cell1$code[["a"]]=5
cell2
# Create grid and copy it
grid <- matrix( sapply(1:(20*20), function(x){
cell(ifelse(runif(1)<= 0.5, 3, 1), hash("a",1))
}), 20, 20)
nextgrid = apply(grid, c(1,2), function(x) x[[1]]$copy())
# Let's again convince ourselves that we did a deep copy
grid[[1,1]]$code["a"] = 5
nextgrid[[1,1]]