R/slimr_template.R
r_template_constant.Rd
Like slimr_template but automatically inserts code to setup variable as a defineConstant() call in the SLiM initialization block.
r_template_constant(var_name, default = NULL, unquote_strings = FALSE)
slimr_template_constant(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.
placemarker if used outside `slim_block`
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(initialize(), {
r_template_constant("pop1", 100)
r_template_constant("pop2", 100)
}),
slim_block(1, early(), {
sim.addSubpop("p1", pop1);
sim.addSubpop("p2", pop2);
})
)
test_sim
#> <slimr_script[2]>
#> block_init:initialize() {
#> defineConstant("pop1", ..pop1..);
#> defineConstant("pop2", ..pop2..);
#> }
#>
#> block_2:1 early() {
#> sim.addSubpop("p1", pop1);
#> sim.addSubpop("p2", pop2);
#> }
#> This slimr_script has templating in block(s) block_init for
#> variables pop1 and pop2.
slim_script_render(test_sim, data.frame(pop1 = c(200, 200), pop2 = c(100, 300)))
#> <slimr_script_coll[2]>
#> <1>
#>
#> block_init:initialize() {
#> defineConstant("pop1", 200);
#> defineConstant("pop2", 100);
#> }
#>
#> block_2:1 early() {
#> sim.addSubpop("p1", pop1);
#> sim.addSubpop("p2", pop2);
#> }
#>
#> <2>
#>
#> block_init:initialize() {
#> defineConstant("pop1", 200);
#> defineConstant("pop2", 300);
#> }
#>
#> block_2:1 early() {
#> sim.addSubpop("p1", pop1);
#> sim.addSubpop("p2", pop2);
#> }