我有一个嵌套的列表结构,我正在使用它作为类的基础。每个类对象都可以包含另一个类对象的实例,它将用于链接一系列命令。我试图重载+
运算符,以便能够迭代地建立一组命令。这需要找到“最深”的命令并附加到它。这是我没有任何运气的地方。追加到任意嵌套列表
# an aribitrarily nested list
tmp <- list(x = list(x = list(x = list())))
# find deepest 'x'
last.x <- function(e) {
while(!is.null(e$x)){
e <- e$x
}
return(e)
}
# I need to be able to append to the deepest 'x' in the list. Ideally:
last.x(tmp)$x <- list()
其产生错误:
Error in last.x(tmp)[["x"]] <- list() :
could not find function "last.x<-"
我正在寻找某种方式通过引用来访问“最深的” x和孩子追加到它。