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),
    predstock = g3_stock('pred', 11:20),
    stock = g3_stock('prey', 1:10))
#>    11:12    12:13    13:14    14:15    15:16    16:17    17:18    18:19 
#> 1.397622 1.767104 1.920011 1.980810 1.999132 1.997659 1.988375 1.975444 
#>    19:20   20:Inf 
#> 1.960940 1.945979 
#> 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)
}