Skip to content

Instantly share code, notes, and snippets.

@robertzk
Created December 6, 2013 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertzk/7833385 to your computer and use it in GitHub Desktop.
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…
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