A 'metafunction' in R to create functions with evaluated/explicit parameter? (Don't know how to phrase this exactly)
Asked Answered
P

1

5

I want to create a function that returns another function with a parameter from the former, e.g.

foo1 <- function(y) function(x) x+y

(Edit: I wrote foo1 <- function(y) function(x+y) originally, changed this. Also, see the answer of @Konrad Rudolph as to why this is a bad definition.)

but! I would like to be able to see what parameter value 'y' was chosen. The above prints like

foo1(1.2)
#> function(x) x + y
#> <environment: 0x000001dd24275a90>

Now, I could achieve this through

foo2 <- function(y){
    eval(parse(text = sprintf("function(x) x + %s", y)))
}

Then printing 'foo2' shows what parameter value 'y' was used in its creation

foo2(1.2)
#> function(x) x + 1.2
#> <environment: 0x000001dd242604a0>

However, I suspect there is a more elegant solution.

I thought something like

foo3 <- function(y){
   eval(substitute(function(x) x+dummy, env = list(dummy=y)))
}

could work, but no. That will print

foo3(1.2)
#> function(x) x+dummy
#> <environment: 0x000001dd23e137d0>

Does anyone have a more elegant solution than my 'foo2'?

Phosphorescent answered 5/9 at 18:51 Comment(0)
A
8

First of all, beware that your original function is broken (beyond the syntax error):

foo1 <- function(y) function(x) x+y

a <- 10
myfoo <- foo1(a)

a <- 20
myfoo(1)
# [1] 21

You probably wanted the last call to return 11, not 21.

To fix this, we need to force eager evaluation of y - otherwise it will be lazily evaluated when it’s used, and not before:

foo1 <- function (y) {
    force(y)
    function (x) x + y
}

Next, to answer your actual question: your foo3 almost works! R just tricks you, because it stores the “source code” of the function in an attribute (srcref) of the function. And when displaying the function definition via print, it prints that srcref (if available) instead of the actual body. We can delete the attribute to fix that:

foo <- function (y) {
    fun <- eval(substitute(function(x) x + dummy, env = list(dummy = y)))
    attr(fun, 'srcref') <- NULL
    fun
}
foo(1.2)
# x + 1.2
# <environment: 0x108952138>

(Note that we don’t need to explicitly force evaluation of y in this definition of foo(), since passing it to list() (and then to substitute()) will cause it to be evaluated anyway.)

Alarcon answered 5/9 at 19:21 Comment(4)
This solution work beautifully! Although I have to use 'attr(fun, "srcref") <- NULL' (maybe this solves the problem for @SamR as well?) . Thanks for this answer - very illuminatingPhosphorescent
@HenrikRenlund Oh, yes, typo! I actually fixed this in my code (I test the code before I post it!) but forgot to update the post.Alarcon
@SamR I guess you probably already had had a look at the definition of print.function but in case you haven’t, also have a look at the difference between print(foo3(1)) and print(foo3(1), useSource = FALSE) (using OP’s definition of foo3).Alarcon
Fascinating - so you could keep the foo3 definition the same as the version by @HenrikRenlund and make R always print functions this way, e.g. print.function <- function(x) print.default(x, useSource = FALSE). Or if you don't want to change the behaviour of printing all functions, instead of setting the "srcref" attribute to NULL, you could do class(fn) <- "printliteral" inside your function and define print.printliteral <- function(x) print.default(x, useSource = FALSE). Interesting stuff.Elodia

© 2022 - 2024 — McMap. All rights reserved.