Created
December 6, 2013 22:41
-
-
Save robertzk/7833385 to your computer and use it in GitHub Desktop.
R exercise: Write a function called fget() that finds only function objects. It should have two arguments, name and env, and should obey the regular scoping rules for functions: if there's an object with a matching name that's not a function, look in the parent. (This function should be a equivalent to match.fun() extended to take a second argum…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fget <- function(name, env = parent.frame(), inherits = TRUE) { | |
if (identical(env, emptyenv())) stop(name, ' not found') | |
if (exists(name, env = env, inherits = FALSE) && is.function(tmp <- env[[name]])) return(tmp) | |
else if (inherits) fget(name, parent.env(env)) | |
else stop(name, ' not found') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment