我希望能够在R中创建一个具有两个“层”功能的对象。我使用S4类在下面汇总了一个非常基本的例子。
Forms.Timer
我的问题是,要使此代码生效,我必须将setClass("2layer_obj",
slots = list(
lower_functions = "list",
higher_functions = "list"
)
)
lower_functions <- list(
function1 = function(x){
x+2
},
function2 = function(y){
y+6
}
)
higher_functions <- list(
function3 = function(z){
test_obj@lower_functions$function1(z)+test_obj@lower_functions$function2(z)
},
function4 = function(z){
test_obj@lower_functions$function1(z)-test_obj@lower_functions$function2(z)
}
)
test_obj <- new("2layer_obj",
lower_functions = lower_functions,
higher_functions = higher_functions
)
rm(lower_functions)
rm(higher_functions)
test_obj@higher_functions$function3(7)
放在test_obj@
之前。因此,如果我重新运行此代码但更改
lower_functions$function1(z)
到
test_obj <- new("2layer_obj",
lower_functions = lower_functions,
higher_functions = higher_functions
)
并尝试运行test_obj2 <- new("2layer_obj",
lower_functions = lower_functions,
higher_functions = higher_functions
)
它不起作用,因为
test_obj2@higher_functions$function3(7)
所以,我的问题是如何在R中创建一个对象(带有任何OO类系统),其中一个槽中的函数可以在另一个槽中运行一个函数,而不必使用该对象的名称?