Function call processing

Hi

I am looking for information about how to make a function aware of its own call.

Let's say I have a function called myf that takes 2 arguments: x and y
myf <- function(x, y) { #some code }.

How can I know within the function:
1- if x and y were assigned to objects or values ?
For example myf( x = a, y = b ) versus myf( x = 1:5, y = data.frame(a = 1, b=2) ),
2- what are the name of the objects passed to x and y if they were assigned to existing objects?
For example myf( x = a, y = b ), I would like to know that x was assigned to a object called "a" and y to an objected called "b"

Thank you

I think you're looking for substitute(), or, for a tidyverse version, the quotation methods described here:

myf <- function(x, y) {
  message("x: ", substitute(x))
  message("y: ", substitute(y))
}

a <- "5"
myf(a,6)
#> x: a
#> y: 6


myf( x = a, y = b ) 
#> x: a
#> y: b


myf( x = 1:5, y = data.frame(a = 1, b=2) )
#> x: :15
#> y: data.frame12

Created on 2025-03-20 with reprex v2.1.1

1 Like

Thanks @AlexisW

I think I need to combine substitute and deparse to get the name of the objects passed to the function.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.