1

I'm searching this for a while and I can't find the answer. Is there some function which gives name of the vector ? It's very important to me to have such. Like in the example following.

 long_name<-c(1,2,3)
    vec_name<-function(vec){} 
    vec_name(long_name) 
    long_name
John
  • 1,849
  • 2
  • 13
  • 23

2 Answers2

3

You could use match.call:

long_name <- c(1, 2, 3)

vec_name <- function(vec) as.character(as.list(match.call())[[2]])

vec_name(long_name) 
#> [1] "long_name"

Created on 2020-08-10 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Kinda interesting, It's really working regardless to vector you put in. How would you do that without function ? It's non intuitive because I don't see that you used vec directly in a function – John Aug 10 '20 at 12:58
  • 1
    @John how do you mean "without function"? If you wanted you could do `as.character(quote(long_name))` – Allan Cameron Aug 10 '20 at 13:03
3

What about substitute?

vec_name <- function(vec) {
  substitute(vec)
} 

which gives

> vec_name(long_name)
long_name
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81