5

I have been reading the Functionals from github. One suggestion in the page was to use call_function if one is working with a list of functions. Here is the code from the page:

call_fun <- function(f, ...) f(...)
f <- list(sum, mean, median, sd)
lapply(f, call_fun, x = runif(1e3))

The output was posted as:

# [[1]]
# [1] 498
# 
# [[2]]
# [1] 0.498
# 
# [[3]]
# [1] 0.49
# 
# [[4]]
# [1] 0.29

However, I was not able to replicate the above results. I got the error:

Error in FUN(X[[4L]], ...) : could not find function "f"

Am I missing something here?

1 Answer 1

5

You have redefined the function sd:

sd = 2

call_fun <- function(f, ...) f(...)
f <- list(sum, mean, median, sd)
lapply(f, call_fun, x = runif(1e3))
#Error in FUN(X[[4L]], ...) : could not find function "f"

Restart your session or do rm(sd).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @eddi. I am not sure whether my question will be useful for future users. I will be happy to delete if it seems trivial.
I'm not sure that you can delete it. It already has an upvote (mine) and upvotes on the answer (mine and anothers). I'm still curious if there is a programmatic way to handle the problem. I tried call_fun <- function(f, ...) match.fun(f)(...) which succeeds in the simple case and fails in the same manner with sd==5 in the environment.
@Metrics I've run into this exact same issue before with sd (there are only so many two letter variable names), so it's probably not too uncommon of a mistake (not so sure though one can find this question through a search engine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.