[SOLVED] Getting the name of the assigned variable to an argument in a function


I’d like to state the variable that has been assigned to an argument in a function. Function is more complex, but an example scenario is below, where I want the function to return the name of the variable: x


x <- 3
my_function <- function(arg1) {print(arg1)}

where: my_function(x) will return x

and: my_function(y) will return y

Is this possible?


Use substitute to return a symbol object. If we wrap with deparse on the substitute, it returns a character

my_function <- function(arg1) substitute(arg1)


> my_function(x)
> my_function(y)

Answered By – akrun

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *