rep_vars: Function to replace variables in function body expr is...

Description Usage Arguments Value Examples

View source: R/expressions.R

Description

Function to replace variables in function body expr is 'body(f)', keyvals is a lookup table for replacements

Usage

1
rep_vars(expr, keyvals)

Arguments

expr

body of function for example, ie 'body(func)'

keyvals

named lookup

Value

new expression with substituted variables

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# replace the formals of a function and its body variables
# http://stackoverflow.com/questions/33850219/change-argument-names-inside-a-function-r
## Not run: 
  f <- function(x, y) -x^2 + x + -y^2 + y
  newvals <- c('x'='x0', 'y'='y0')      # named lookup vector
  newbod <- rep_vars(body(f), newvals)  # create new body
  # Rename the formals, and update the body
  formals(f) <- pairlist(x0=bquote(), y0=bquote())
  body(f) <- newbod

## End(Not run)

nverno/stuff documentation built on May 23, 2017, 5:58 p.m.