0

I am struggling to find a way to capture the assignment name of a new object initialized in R. Below is a very simplistic example with some pseudo-code to demonstrate my problem.

f <- function(){
    print(nameOfObject)
}

# I would like the following to be returned
a <- f()
[1] "a"

I have poked around in the docs but I have only figured out how to get the name of an 'object' passed in to the function as opposed to the name of the new object. Not sure if this is possible.

cdeterman
  • 19,630
  • 7
  • 76
  • 100
  • `f <- function(x){deparse(substitute(x))}`. – Rui Barradas Nov 03 '17 at 16:49
  • I think you are confused about the semantics of R functions. The `<-` operator is a function and you are executing `<-(a, f() )`. That call would not result in `a` being passed to the `f`-function. If what Rui suggests then this is just a duplicate. https://stackoverflow.com/questions/10520772/in-r-how-to-get-an-objects-name-after-it-is-sent-to-a-function/10520832#10520832 – IRTFM Nov 03 '17 at 16:49
  • 1
    The right-hand side of `<-` will be evaluated before it sees the left-hand side, so it doesn't seem possible unless you redefine the assignment operator... – Frank Nov 03 '17 at 16:50
  • @42 I suspected that `a` wouldn't be passed to the function. I just wanted to be sure there wasn't a way. What Rui is suggesting is what I have seen but isn't what I am looking for. – cdeterman Nov 03 '17 at 16:52
  • Then you would need to redefine `<-` as Frank said. (Not likely to be a good solution to the unstated real question.) – IRTFM Nov 03 '17 at 16:54

0 Answers0