我喜欢以这种方式更改以下 src,初始化函数看起来像某些构造函数。我想更改以下示例
Part.initialize<-function(...){
args<-list(...)
if(all(class(args[[1]])==c("XMLInternalElementNode", "XMLInternalNode", "XMLAbstractNode"))){
attr<-xmlAttrs(node)
.self$var1 <- if(is.na(attr["var1"])) vector() else attr["var1"]
.self$var2 <- if(is.na(attr["var2"])) character() else as.character(attr["var2"])
}else{
.self$var1 <- if(is.null(args[["var1"]])) vector() else args[["var1"]]
.self$var2 <- if(is.null(args[["var2"]])) character() else as.character(args[["var2"]])
}
.self
}
Part<-setRefClass(Class = "Part"
,fields = c(var1 = "ANY", var2 = "character")
,methods = list(initialize=Part.initialize))
txt = "<doc> <part var2=\"abc\"/> </doc>"
tree <- xmlTreeParse(txt, useInternalNodes = TRUE)
node<-xpathSApply(tree, "//part")[[1]]
part <- Part$new(node)
类似于:
Part.initialize<-function(XMLNode){
do something
}
Part.initialize<-function(var1=c(),var2=character()){
do something
}
除了如何处理 ANY 类型的默认值?直到现在我使用向量()。