R/evals.R

Defines functions evals

Documented in evals

#' @title Function \code{evals}
#' @description Evaluate a character vector as a bunch of expressions.
#' @seealso \code{\link{eply}}, \code{\link{help_eply}}
#' @export
#' @return a list or vector of return values of \code{.fun}.
#' @param x character vector of expressions to evaluate
#' @param .with list, data frame, or environment with the
#' data accessible to the expressions in \code{x}
#' @param .simplify \code{TRUE} to simplify the result
#' and \code{FALSE} otherwise
#' @examples
#' # Get an example list of supporting data. Could be an environment.
#' .with <- example.with()
#' # Row-by-row, evaluate the code in .expr and feed the results to the function.
#' evals(x = c("a + 1", "b + 2"), .with = .with)
evals <- function(x = NULL, .with = parent.frame(), .simplify = TRUE) {
  if (!length(x)) return()
  checks_evals(.with = .with, .simplify = .simplify)
  force(.with)
  out <- vevals(x, .with = .with)
  names(out) <- x
  if (.simplify) out <- simplify2array(out)
  out
}

# evals but with no names
vevals <- Vectorize(
  FUN = function(x, .with) {
    eval(parse(text = as.character(x)), envir = .with)
  },
  vectorize.args = "x",
  SIMPLIFY = FALSE,
  USE.NAMES = TRUE
)
wlandau/eply documentation built on May 29, 2019, 10:39 a.m.