R/node_identity.r

Defines functions is_identity_node node_identity

Documented in node_identity

## a node that simply evaluates an expression on variables already present
## in the data, such as ~ A + B + 4
#' @export
node_identity <- function(data, parents, formula) {

  # parse formula to string, remove leading ~
  form_str <- paste0(str_trim(deparse(formula)), collapse="")
  form_str <- substr(form_str, start=2, stop=nchar(form_str))

  # evaluate expression on data
  out <- with(data, eval(str2lang(form_str)))

  return(out)
}

## check if supplied node type is a node of type "identity"
is_identity_node <- function(node) {
  (length(node)==1 && is.character(node) && node=="identity") ||
  (is.function(node) && extract_function_name(node)=="node_identity")
}

Try the simDAG package in your browser

Any scripts or data that you put into this service are public.

simDAG documentation built on April 3, 2025, 10:35 p.m.