访问存储在 R 列表中的对象槽

Access slots of objects stored in list in R

以下代码产生错误:

setClass(Class ='Foo', slots=c(field_1='character', field_2 = 'character' ))
list_of_obj <- c(new('Foo', field_1 = 'bar', field_2 = 'foo_bar'))
list_of_obj[1]@field_1


Error: trying to get slot "field_1" from an object of a basic class ("list") with no slots

如何访问存储在列表中的对象中的插槽 field_1

使用双括号访问列表的元素, 单括号会给你一个列表对象

list_of_obj[[1]]@field_1
[1] "bar"