How to convert variable (object) name into String

StringRObject

String Problem Overview


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 ()
   }

String Solutions


Solution 1 - String

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

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

myfunc(foo)
[1] "foo"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneversaintView Question on Stackoverflow
Solution 1 - StringSven HohensteinView Answer on Stackoverflow