R을 사용하여 큰 JS 개체 (라이브러리 rjsonio 사용)로 작업하고 있습니다. 따라서 중첩 목록이 많아서 작업하기가 다소 번거롭습니다. 아래에 간단한 예제가 있습니다. 나는 'getter'와 'setter'함수의 어떤 형태를 만들어서이 객체로 작업하려고 노력하고있다. 둘러보고 난 후에, 나는 객체를 재귀하고 첫 번째로 일치하는 레이블을 반환하는 꽤 좋은 'getter'함수를 발견했다. 이것은 함수를 연결하는 데 적합하기 때문에 특히 좋습니다. 그러나, 나는 'setter'기능에 대해 동일한 효과를 얻는 방법을 이해할 수 없다. 유사한 방식으로 함께 묶일 수있는 '세터'기능을 만드는 방법에 대한 의견이 있으십니까?여러 대체 함수 연결 R
#example, simplified, object
app = list(
1,
2,
d=list(a=123,
b=456,
list(
FirstKey=list(attr1='good stuff', attr2=12345),
SecondKey=list(attr1='also good stuff', attr2=4321)
)
)
)
#Return a function that returns the value
#associated with first label that matches 'name'
getByName <- function(name){
rmatch <- function(x) {
pos <- match(name, names(x))
if (!is.na(pos))
return(x[[pos]])
for (el in x) {
if (class(el) == "list") {
out <- Recall(el)
if (!is.null(out)) return(out)
}
}
}
rmatch
}
getFirstKey <- getByName("FirstKey")
getAttr1 <- getByName("attr1")
getAttr2 <- getByName("attr2")
#I like that I can chain these functions together
getAttr1(getFirstKey(app))
getAttr2(getFirstKey(app))
# I would like to be able to do something like this
# But this won't work
### getAttr1(getFirstKey(app)) <- 9876
# This does work,,, but I loose the ability to chain functions together
# Closure around a replacement function
setterKeyAttr <- function(keyName, attr){
function(x, value){
x$d[[3]][[keyName]][[attr]] <- value
x
}
}
`setFirstKeyAttr2<-` <- setterKeyAttr("FirstKey", "attr2")
setFirstKeyAttr2(app) <- 22222
#check the answer is correct
getAttr2(getFirstKey(app))
참조 : R decorator to change both input and output
http://r.789695.n4.nabble.com/How-to-get-a-specific-named-element-in-a-nested-list-td3037430.html
http://adv-r.had.co.nz/Functions.html