-1

I am trying to parse out objects from a collection of objects when using the R programming language. My code sample below shows that the collection has data. However, I can't figure out how to extract and store data for a single object.

Here is a complete code sample which does run:

MyNewClass <- setRefClass(
"MyNewClass", fields  = list( team   = "character", player = "character" )
)

Output <- setRefClass(
  "Output",
  fields  = list(myVar = "data.frame"),
  methods = list(

    setData = function(teamNames,participants) {
      lenTitles    = length(teamNames)
      print(lenTitles)
      lenDesc      = length(participants)
      myCollection<-c()
      for(i in 1:lenTitles) {
        myobj <- structure(list(title = teamNames[i], 
                                description = participants[i]), class = "myclass")
        myNewClass        <- MyNewClass$new()
        myNewClass$team   <- teamNames[i];
        myNewClass$player <- participants[i];
        myCollection <- c(myCollection, myNewClass)
      }
      print("** Collection Data **")
      print(myCollection)           # This works

      print("** Single Object Property **")
      print(myCollection[1]$team)   # This does not work.
    }
  )
)

MyClass <- setRefClass(
  "MyClass",
  fields  = list(myVar = "numeric"),
  methods = list(
    myInitializer = function() {
      teamNames       <- c("A Team", "B Team")
      participants    <- c("Aaron Atkinson", "Barbara Bellemont")
      output<-Output$new()
      output$setData(teamNames, participants)
    }
  )
)

myObject<- MyClass$new(myVar = 1)
myObject$myInitializer()

The output shows that the collection has two objects but I have been unable to extract an object from the collection:

[1] 2
[1] "** Collection Data **"
[[1]]
Reference class object of class "MyNewClass"
Field "team":
[1] "A Team"
Field "player":
[1] "Aaron Atkinson"

[[2]]
Reference class object of class "MyNewClass"
Field "team":
[1] "B Team"
Field "player":
[1] "Barbara Bellemont"

[1] "** Single Object Property **"
NULL

Thank you for your suggestions.

Pat M
  • 44
  • 5
  • 1
    Dear @CyrusMohammadian (and all others who may have downvoted this question), the OP has explicitely stated "*Here is a complete code sample which does run*". I was able to reproduce the problem by copying the code snippet to R. So, there is no reason to complain about missing data to reproduce the problem or to downvote the Q. I find this is a well written Q which should be upvoted. (Although I must admit the title is somewhat misleading as it uses some *non-R* terms. A better one could be *Accessing list elements in R*.) – Uwe Aug 30 '16 at 06:06
  • @Uwe, I stand corrected but I didn't down vote (nor do I for lack of data) – Cyrus Mohammadian Aug 30 '16 at 06:10
  • 1
    @CyrusMohammadian Thank you for your positive reaction. It seems that some SO users are rather quick in downvoting when they find negative comments. Unfortunately, there is no way (known to me) to tell them they should review their votes. So, the downvotes stay for ever. – Uwe Aug 30 '16 at 06:20
  • @Uwe thanks for pointing this out btw, this is true and im guilty of that myself sometimes, specially when running through tons of posts. I don't remember if I had seen the down vote first and made the comment subconsciously (or consciously) or someone saw my comment and made the down vote -I guess folks use other people's actions as heuristics, which isnt good but we're human after all. – Cyrus Mohammadian Aug 30 '16 at 06:26
  • Uwe and Cyrus...I really appreciate you taking the time to go through this. Thank you for your efforts to keep this forum a great one. – Pat M Aug 30 '16 at 09:40

1 Answers1

2

Use double brackets to access a single list element. With

print("** Single Object Property **")
      print(myCollection[[1]]$team)   # This works

you will get:

[1] "** Single Object Property **"
[1] "A Team"

Explanation

According to section 3.4 of the R Language Definition

For lists, one generally uses [[ to select any single element, whereas [ returns a list of the selected elements.

Please, see also this answer on SO.

Community
  • 1
  • 1
Uwe
  • 41,420
  • 11
  • 90
  • 134
  • Uwe, thank you so much. I had spent many hours over a period of days trying to solve this with no success and I had done my best to search the web. Thank you for highlighting the documentation. The solution is clear now that you point it out. (I am a newbie with R.) This is a big help. – Pat M Aug 30 '16 at 09:38