RoyalTS
RoyalTS

Reputation: 10203

Variable name in calling scope

I have a function that takes a single variable as its argument:

calling_variable_name <- function(var) {
   ...
}

callling_variable_name(calling_variable)

What the function is supposed to do is return the name of the variable the function is called with, calling_variable in this case. I know I can get the name of the variable within the function using quote().

Upvotes: 0

Views: 70

Answers (1)

HFBrowning
HFBrowning

Reputation: 2336

You never really asked a question - but the function that accomplishes what you'd like to do is substitute():

calling_variable_name <- function(var){
  return(substitute(var))
}

calling_variable = 2
calling_variable_name(calling_variable)
# prints literal: calling_variable (and not 2)

Upvotes: 1

Related Questions