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.