Evaluate G3 formulas / code outside a model

g3_eval(f, ...)

Arguments

f

A formula object or quoted code to be evaluated

...

Named items to add to the formula's environment, or a single list / environment to use.

Details

Allows snippets of gadget3 code to be run outside a model. This could be done with regular eval, however, g3_eval does a number of things first:

  1. The global g3_env is in the environment, so functions such as avoid_zero can be used

  2. If substituting a g3_stock, all definitions such as stock__minlen will also be substituted

  3. g3_param('x') will pull param.x from the environment

Value

Result of evaluating f.

Examples

# Evaluate suitiability function for given stocks
g3_eval(
    g3_suitability_andersen(0,1,2,3,4),
    predator_length = 100,
    stock = g3_stock('prey', 1:10))
#>        1:2        2:3        3:4        4:5        5:6        6:7        7:8 
#> 0.06590394 0.17962624 0.31617501 0.45914780 0.60006286 0.73464024 0.86084911 
#>        8:9       9:10     10:Inf 
#> 0.97788544 1.08562040 1.18429375 
#> attr(,"class")
#> [1] "force_vector" "numeric"     

# Parameters can be filled in with "param." items in environment
g3_eval(quote( g3_param('x') ), param.x = 88)
#> [1] 88
g3_eval(
    g3_parameterized('lln.alpha', by_stock = TRUE, value = 99),
    stock = g3_stock("fish", 1:10),
    param.fish.lln.alpha = 123)
#> [1] 123

# Graph gadget3's built-in logspace_add()
if (interactive()) {
  curve(g3_eval(quote( logspace_add(a, 10) ), a = x), 0, 50)
}