1

ann <- Person$new("Ann", "black")

In the example above (which is from this Introduction), how would I get "ann"?

For instance, I would need a method ann$getName that would return "ann".

ariel
  • 11
  • 5
  • Why do you need this? – HFBrowning Nov 18 '17 at 01:35
  • Because I'm assuming you mean the var name you assigned the object to - not the value of "name" (which you can get by typing `ann$name`) – HFBrowning Nov 18 '17 at 01:36
  • Yes, I need the var name I assigned the object to. I need it to create a log of commands used involving my R6 class. – ariel Nov 18 '17 at 01:42
  • Do you mean `obj <- get("ann")`? – Maurits Evers Nov 18 '17 at 01:44
  • No, I just need the variable "ann". After I instantiate an object, how do I get the variable name? – ariel Nov 18 '17 at 01:45
  • On the R6 documentation, I can check that ann is an R6 class using isR6("ann"). However, is there a method to return the name of the instantiation, e.g. ann$returnName and returnName would return "ann" – ariel Nov 18 '17 at 01:48
  • 1
    I think you should perhaps provide more code about what you're trying to do with logging. What you're asking for to me sounds like a code smell but it could be that I don't really understand what you want – HFBrowning Nov 18 '17 at 01:50

1 Answers1

0

I'm trying to do the same thing, so perhaps I can clarify the question. The goal (for me) is to give the user of the class some feedback. Something like that:

Person <- R6Class("Person",
  public = list(
    name = NULL,
    hair = NULL,
    initialize = function(name = NA, hair = NA) {
      self$name <- name
      self$hair <- hair
       },

    do_something_very_long=function(){
      whoami <- self$getInstanceName() ## $getInstanceName() is the method I need to write !
      message(paste("Please wait, processing object",whoami))
      # Do a very long calculation... 
     }
  )
)

Which I'd then run in a script that would do something like

#File batch_processing.R
first_in_line<-Person$new("Alice","Black")
next_customer<-Person$new("Bob","Red")
VIP<-Person$new("Charlie","Brown")
# etc ...

first_in_line$do_something_very_long()
next_customer$do_something_very_long()
VIP$do_something_very_long()
# etc ...

So, my (notional) user will start batch_processing.R, perhaps with

$ R ~/batch_processing.R 

or

R> source("batch_processing.R")

and watch not much happening while the script works. I would like, therefore, some feedback so that when the user comes back after his coffee, he can look at the screen and see that the computer is busy processing VIP or next_customer.

Obviously - one way is to explicitly give unique identifiers to each object ($name in this case). In my real application case, however, this would not be very meaningful, or would duplicate the object name ("model 1", "model 2"...) which is a bit wasteful !

jfmoyen
  • 495
  • 2
  • 11
  • Wait - I cannot really do that, because several names may be potentially bound to the same object (the R6 object in that case), right ? (cf. https://adv-r.hadley.nz/names-values.html) More generally, is there a way to get the list of all symbols pointing to a given object ? – jfmoyen Jul 20 '20 at 05:24