1

I have read In R, how to get an object's name after it is sent to a function? and have a similar question. The above gets the object name after it is defined. What I want is to get the name while it is being defined in a function i.e. print the object name while the function is being called.

The thing that is different is that I want to find the name of the object before it is defined in the local environment.

I want my function foo to print the object name in the output as well. So my function foo would look something like this.

foo<-function(x) {
  print('name of object that is calling foo')
  return(x)  
}

This will return the character string "name of object that is calling foo" and 1. What I want is for the function to print "object" and 1 when I call object = foo(1). I know that match.call() returns the function and arguments but I cant find a function that will return the object name that is currently calling the function.

A plausible workaround that I can think of would go through the history and print the first object that matches the match.call() but I hope that theres a simpler way to do this.

Feel free to ask me for any clarification.

Tan Jason
  • 67
  • 1
  • 8
  • I don't understand what you're trying to do. Function `foo` will never get to the `print` statement because of the `return` statement. What do you want to print? Also, `print(?)` is not valid R code. – Maurits Evers Nov 20 '17 at 05:36
  • Could you also clarify how you're question is different from the one [you link to](https://stackoverflow.com/questions/10520772/in-r-how-to-get-an-objects-name-after-it-is-sent-to-a-function) in the beginning of your post. – Maurits Evers Nov 20 '17 at 05:39
  • the statement after `return` is not reachable anyway, All other commands should be before `return` statement. Can you say what is your expected output please? – SirSaleh Nov 20 '17 at 05:40
  • okay thanks for all your input. I have edited the question and I hope it is clearer now. – Tan Jason Nov 20 '17 at 05:46
  • I don't think that will work, nor can I think of a situation where that would be useful. But perhaps I misunderstood. Function `foo` and `object` reside in different scopes. The output of `foo(1)` will be assigned to `object`, after execution of function `foo`. – Maurits Evers Nov 20 '17 at 06:01
  • I am working on a collaboration with some friends and we are dealing with s4 classes. I was thinking to put the minimal code needed to generate the s4 class in one of its slots. So `class@minimalcode` will return something like a cat of `object=match.call()` which automatically generates new lines whenever we call any method of the class so that we can keep track of how the class is being created and being redefined. Yes. My guess is that the name of the object will not be defined in any environment while the function is being run so im out of luck here :( – Tan Jason Nov 20 '17 at 06:15
  • Yes, as I said, you are *assigning* the output of a function `foo()` to a variable `obj` *after* the function has been called. I don't see a way for `foo` to know about `obj` beforehand. One way could be to explicitly pass the name as a function argument, so e.g. `obj <- foo(1, "obj")`, in which case you can access the second argument from within `foo`. – Maurits Evers Nov 20 '17 at 07:55

0 Answers0