eval.Rd
Evaluate G3 formulas / code outside a model
g3_eval(f, ...)
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:
The global g3_env
is in the environment, so functions such as avoid_zero
can be used
If substituting a g3_stock
, all definitions such as stock__minlen
will also be substituted
g3_param('x')
will pull param.x
from the environment
Result of evaluating f.
# 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)
}