Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative to current evalPopArg #108

Open
WetRobot opened this issue Jun 8, 2016 · 0 comments
Open

Alternative to current evalPopArg #108

WetRobot opened this issue Jun 8, 2016 · 0 comments
Assignees

Comments

@WetRobot
Copy link
Collaborator

WetRobot commented Jun 8, 2016

...is dividing the large function into methods for new generic evalArg:

evalArg <- function(arg, ...) {
  UseMethod("evalArg")
}


evalArg.default <- function(arg, env, enc) {
  if (is.list(arg)) arg <- as.list(arg)
  return(arg)
  stop("No method found for object having class(es) ", 
       paste0(class(arg), collapse = ", "), " for generic evalArg.")
}

evalArg.name <- function(arg, env, enc) {
  d <- deparse(arg)
  out <- try(get(d, envir = env, inherits = FALSE), silent = TRUE)
  if (inherits(out, "try-error")) {
    out <- try(get(d, envir = enc, inherits = FALSE), silent = TRUE)
  }
  if (inherits(out, "try-error")) {
    stop("Could not find object ", d, ".")
  }
  out <- list(out)
  names(out) <- d
  out
}

evalArg.call <- function(arg, env, enc) {

  out <- eval(arg, env = env, enc = enc)
  if (is.list(out)) {
    out <- as.list(out)
  } else if (mode(out) %in% c("numeric", "character") || is.vector(out)) {
    out <- list(out)
    names(out) <- paste0(deparse(arg), collapse = "")
  }
  out
}

evalArg.character <- function(arg, env) {
  se <- substitute(env)
  out <- try({
    get(arg, envir = as.environment(env), inherits = FALSE)
  }, silent = TRUE)
  if (inherits(out, "try-error")) {
    stop("Could not find object ", arg, ".")
  }
  out <- list(out)
  names(out) <- arg
  out
}

evalArg.formula <- function(arg, env) {


  rhsc <- attr(terms(f), "term.labels")

  rhsl <- lapply(rhsc, function(stri) parse(text = stri)[[1]])

  names(rhsl) <- rhsc

  rhsl <- lapply(rhsl, eval, env = env, enc = attr(arg, ".Environment"))

  ## TODO: handle : operator as if x and y are both factors

  rhsl

}







Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant