-1

I have made 4 variables that represents 4 collections: zk1, zk2, zk3, zk4. Using mongolite:

for (i in 1:4) {
  name <- paste0("zk", i, sep = "")
  collect <- paste0("zk", i, sep = "")
  assign(name, mongo(collection = collect, db = "zeko", url = "mongodb://localhost"))
}

Now, in next stages I would like to loop through variables when applying some method. I tried different ways but it always return an error. For example:

for (j in 1:length(zk_vector)) {
  j$count()
}

returns an error:

Error in j$count : $ operator is invalid for atomic vectors

I have tried other solutions but unsuccessfully.

Mislav
  • 1,533
  • 16
  • 37

1 Answers1

0

Try eval(parse(text = paste0(name,"$count"))). You should check that name is in your environment and that you can access realnamehere$count. I assume it is not a functioncount()

Christoph
  • 6,841
  • 4
  • 37
  • 89
  • I is a method count(). If I try your solution it gives me the description of the function. – Mislav Jul 15 '16 at 16:15
  • 2
    I think there is some misconception about variables and functions. But I can only help you with a reproducable example: Please read [(1)](http://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](http://stackoverflow.com/help/mcve) How to create a MCVE as well as [(3)](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. – Christoph Jul 18 '16 at 07:45