I am a little surprised by the behavior of R in a very specific case. Let's say I define a square function that returns the square of its argument, for example:
square <- function(x) { return(x^2) }
I want to call this function in another function, and I also want to display its name when I do this. I can do this with deparse(substitute()) . However, consider the following examples:
ds1 <- function(x) { print(deparse(substitute(x))) } ds1(square)
This is the expected result, so everything is in order. However, if I pass a function wrapped in a list and process it with a for loop, the following will happen:
ds2 <- function(x) { for (y in x) { print(deparse(substitute(y))) } } ds2(c(square)) # [1] "function (x) " "{" " return(x^2)" "}"
Can someone explain to me why this is happening and how I could prevent this?
r
A. Stam
source share