Search code examples
rlistobjectr-s4slot

Access slots of objects stored in list in R


The following code produces error:

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

How can I access slot field_1 in my object stored in list?


Solution

  • use the double bracket to access the elements of the list, single brackets will give you a list object

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