157

I have the following data frame with variable name "foo";

 > foo <-c(3,4);

What I want to do is to convert "foo" into a string. So that in a function I don't have to recreate another extra variables:

   output <- myfunc(foo)
   myfunc <- function(v1) {
     # do something with v1
     # so that it prints "FOO" when 
     # this function is called 
     #
     # instead of the values (3,4)
     return ()
   }
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
neversaint
  • 60,904
  • 137
  • 310
  • 477
  • 4
    Just curious - why do you need to get object name from an object? – CHP Jan 29 '13 at 07:21
  • 4
    I have a sample use: I have a function that takes as argument a vector, and append the values of that vector into a column in a dataframe. I also need to populate into another column the source of the value, which is the name of the initial vector. Voila. – Ricky Jan 27 '15 at 03:46
  • 1
    To use `exists()` which requires a string. – Elin Feb 05 '17 at 14:08
  • 3
    @ChinmayPatil It may be useful to pass the string name of the object to its output file name, for instance. – fred Apr 11 '19 at 21:09
  • My two cents: a plotting function using the given variable names as labels / legend – annhak Sep 06 '21 at 07:37

1 Answers1

306

You can use deparse and substitute to get the name of a function argument:

myfunc <- function(v1) {
  deparse(substitute(v1))
}

myfunc(foo)
[1] "foo"
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • +1. Thanks for the helpful answer, what about if I pass `foo[1]`; is there a way to get just "foo" back? – Mahdi Jadaliha Feb 23 '15 at 21:52
  • 2
    @MahdiJadaliha You can try this function: `myfunc <- function(v1) { s <- substitute(v1); if (length(s) == 1) deparse(s) else sub("\\(.", "", s[2]) }`. – Sven Hohenstein Feb 24 '15 at 07:39
  • 4
    How would you do this with multiple objects? Specifically, how would you do it in a way to get separate strings for each object name? (For example, if I had object foo, foo1, and foo2 and I wanted to create a list of their names as separate character strings). – theforestecologist Feb 25 '16 at 18:46
  • 2
    @theforestecologist If the function has multiple parameters, you can use `deparse(substitute(.))` for each parameter, store the result in a variable, and put the variables in a list afterwards. – Sven Hohenstein Feb 27 '16 at 07:59
  • 9
    @SvenHohenstein this doesn't work when you use a for loop... – SumNeuron Oct 30 '16 at 15:39
  • 2
    you can also use deparse(quote(var)) in which quote freeze the var from evaluation and deparse which is the inverse of parse make the symbol back to string – cloudscomputes Jan 30 '18 at 07:54
  • 1
    @theforestecologist the best I've found is : `unlist(strsplit(gsub("^.*\\(|\\)|\\s","", deparse(substitute(x))), ","))` – Salix Jan 27 '21 at 17:28
  • actually see : [this](https://stackoverflow.com/questions/16951080/can-lists-be-created-that-name-themselves-based-on-input-object-names/16951524#16951524) and [that](https://stackoverflow.com/questions/16951080/can-lists-be-created-that-name-themselves-based-on-input-object-names/16951524#16951524) – Salix Jan 27 '21 at 17:54