equation2function: Turn a Character String of the Right-Hand Side of an Equation...

View source: R/equation2function.R

equation2functionR Documentation

Turn a Character String of the Right-Hand Side of an Equation into an R Function

Description

equation2function converts the right-hand side of an equation that can be written as a single line (like the right-hand side of an object of class formula) and creates an R function which calls the variables within as arguments and returns values consistent with the parameters of the input equation as written.

Usage

equation2function(equation, envir = parent.frame(), notName = "XXXXXXXXXXX")

Arguments

equation

The right-hand-side (RHS) of an equation, given as a character string. If not of type character, equation2function attempts to coerce equation to type character and fails if it cannot.

envir

The environment the resulting function will be evaluated in. See as.function.

notName

A useless string used simply as a placeholder in turning equation into a function, which should not match any actual variable in equation. Only supplied as an argument in case any

Details

This simple little function is rather 'hacky' but seems to get the job done, for a functionality that does not seem to be otherwise exist elsewhere in R.

Value

A function, with named blank (i.e. no default value) arguments.

Author(s)

David W. Bapst

Examples

# some simple examples
foo <- equation2function("x+y")
foo
foo(x = 4,y = 0.1)

foo <- equation2function("x+2*sqrt(2*y+3)^2")
foo
foo(x = 4,y = 0.1)

# what about weird long argument names and spaces
foo <- equation2function("stegosaur + 0.4 * P")
foo
foo(stegosaur = 5,P = 0.3)

paleotree documentation built on Aug. 22, 2022, 9:09 a.m.