Creating a function with a FUN input in r

Viewed 447

I am looking to make a function where the user can enter their own model selection function as an input to be used. I'm having trouble with finding the answer as I keep getting search results about how to make a simple R function, as opposed to an input much like the apply family.

Here is an example similar to what I am looking for but not quite:

simple<- function(mod, FUN){
  switch(FUN,
         AIC = AIC(mod),
         BIC = BIC(mod))
}
simple(lm(rnorm(100) ~ rnorm(100,4)), "AIC")

The above code runs but I must plan for all of the possible functions and write them within switch. I also am forced to write "AIC" as opposed to simply AIC.

Any thoughts to how I can create the function I am looking for? Let me know if you need additional information.

1 Answers
Related