定义返回任意数量字段的函数的正确方法是什么。
A.getFields<-function(values){
vars<-getRefClass()$fields()
idx<-names(vars) %in% values
if(length(fields)<1)
return(vars)
return(vars[idx])
}
A.setFields<-function(...){
dots <- list(...)
fieldNames <- names(dots)
for(i in seq_along(dots))
assign(fieldNames[[i]], dots[[i]], attr(.self, ".xData"))
}
A<-setRefClass(Class = "A",
fields = c(var1 = "list",
var2="character"),
methods = list(getFields=A.getFields
,setFields=A.setFields
,initialize=function(...,var1=list(), var2=character()) {
obj<-list(...)[[1]]
if(is(obj,"list"))
do.call(.self$setFields,obj)
else{
.self$var1<-as.list(var1)
.self$var2<-as.character(var2)
}
.self
})
)
a<-A(var1=list(1:3),var2="A")
a$getFields(c("var2"))
a$getFields(c("var2","var1"))