Create a templated variable inside a SLiM script. This function can be used directly inside a slim_block
function call,
to generate a placemarker that can be replaced with different values dynamically using slim_script_render
.
r_template(var_name, default = NULL, unquote_strings = FALSE)
slimr_template(var_name, default = NULL, unquote_strings = FALSE)
Name to use as a placemarker for the variable. This name will be used for replacing values later.
A default value to be inserted during script rendering if a value is not otherwise provided.
If the value being inserted is of class character, should it be 'unquoted', that is,
should double quotes around the value be removed? This is useful when you want to refer to a SLiM object, e.g.
to insert p1.setMigrationRate(...)
instead of "p1".setMigrationRate(...)
, the latter of which is not
valid SLiM code.
Returns the placemarker if used outside
Note that this function is only designed to be used inside a slim_block
function call. If run in any other
situation, it won't really do anything, just returning a reference to the placemarker that would have been inserted if run in
its correct context.
test_sim <- slim_script(
slim_block_init_minimal(mutation_rate = r_template("mut_rate", 1e-7)),
slim_block(1, early(), {
sim.addSubpop("p1", r_template("p1", 100));
})
)
test_sim
#> <slimr_script[2]>
#> block_init:initialize() {
#> initializeMutationRate(..mut_rate..);
#> initializeMutationType("m1", 0.5, "f", 0);
#> initializeGenomicElementType("g1", m1, 1);
#> initializeGenomicElement(g1, 0, 1e+05 - 1);
#> initializeRecombinationRate(1e-08);
#> }
#>
#> block_2:1 early() {
#> sim.addSubpop("p1", ..p1..);
#> }
#> This slimr_script has templating in block(s) block_init and
#> block_2 for variables mut_rate and p1.
slim_script_render(test_sim, data.frame(p1 = c(100, 200),
mut_rate = c(1e-7, 1e-8)))
#> <slimr_script_coll[2]>
#> <1>
#>
#> block_init:initialize() {
#> initializeMutationRate(1e-07);
#> initializeMutationType("m1", 0.5, "f", 0);
#> initializeGenomicElementType("g1", m1, 1);
#> initializeGenomicElement(g1, 0, 1e+05 - 1);
#> initializeRecombinationRate(1e-08);
#> }
#>
#> block_2:1 early() {
#> sim.addSubpop("p1", 100);
#> }
#>
#> <2>
#>
#> block_init:initialize() {
#> initializeMutationRate(1e-08);
#> initializeMutationType("m1", 0.5, "f", 0);
#> initializeGenomicElementType("g1", m1, 1);
#> initializeGenomicElement(g1, 0, 1e+05 - 1);
#> initializeRecombinationRate(1e-08);
#> }
#>
#> block_2:1 early() {
#> sim.addSubpop("p1", 200);
#> }