在 R 中访问列表中的元素

cha*_*142 3 r

我可以制作一个包含函数的列表,例如,

foo <- list(value=1, func=function(x) x+1)
Run Code Online (Sandbox Code Playgroud)

然后foo$func(3)给出4. 但是该函数是否可以$func访问$value同一列表中的元素?我尝试过以下方法,(显然)是错误的:

foo <- list(value=1, func=function(x) x+value)
foo$func(3)
# Error in foo$func(3) : object 'value' not found
Run Code Online (Sandbox Code Playgroud)

我知道以下代码可以工作:

bar <- list(value=1, func=function(FOO,x) x+FOO$value)
bar$func(bar, 3)
# [1] 4
func <- function(FOO,x) x+FOO$value
func(foo,3)
# [1] 4
Run Code Online (Sandbox Code Playgroud)

但由于某些原因,我想使用foo$func(3)语法而不是func(foo,3)。R可以实现这个功能吗?

谢谢。

编辑

除了下面有用的答案之外,?ReferenceClasses也很有用。

Rol*_*ASc 5

foo <- list(value = 1, func = function(x) x + foo$value)
foo$func(3)
Run Code Online (Sandbox Code Playgroud)

这对你来说足够好吗?