Repeat a ParamSet
a given number of times and thus create a larger ParamSet
.
By default, the resulting parameters are prefixed with the string "repX.", where
Xcounts up from 1. It is also possible to tag parameters by their original name and by their prefix, making grouped retrieval e.g. using
$get_values()` easier.
Arguments
- set
- times
(
integer(1)
)
Number of times to repeatset
. Should not be given ifaffixes
is provided.- affixes
(
character
)
Acharacter
vector indicating the prefixes / postfixes to use for each repetition ofset
. Per default, these are prefixes; ifpostfix
isTRUE
, these values are postfixed instead. If this is given,times
is inferred fromlength(affixes)
and should not be given separately. Iftimes
is given, this defaults to"repX"
, withX
counting up from 1.- postfix
(
logical(1)
)
Whether to useaffixes
as a postfix instead of a prefix. DefaultFALSE
(use prefixes).- tag_sets
(
logical(1)
)
Whether to add a tag of the form"set_<affixes[[i]]>"
to each parameter in the result, indicating the repetition each parameter belongs to.- tag_params
(
logical(1)
)
Whether to add a tag of the form"param_<id>"
to each parameter in the result, indicating the original parameter ID insideset
.
Examples
pset = ps(
i = p_int(),
z = p_lgl()
)
ps_replicate(pset, 3)
#> <ParamSet(6)>
#> id class lower upper nlevels default value
#> <char> <char> <num> <num> <num> <list> <list>
#> 1: rep1.i ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 2: rep1.z ParamLgl NA NA 2 <NoDefault[0]> [NULL]
#> 3: rep2.i ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 4: rep2.z ParamLgl NA NA 2 <NoDefault[0]> [NULL]
#> 5: rep3.i ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 6: rep3.z ParamLgl NA NA 2 <NoDefault[0]> [NULL]
ps_replicate(pset, affixes = c("first", "last"))
#> <ParamSet(4)>
#> id class lower upper nlevels default value
#> <char> <char> <num> <num> <num> <list> <list>
#> 1: first.i ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 2: first.z ParamLgl NA NA 2 <NoDefault[0]> [NULL]
#> 3: last.i ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 4: last.z ParamLgl NA NA 2 <NoDefault[0]> [NULL]
ps_replicate(pset, affixes = c("first", "last"), postfix = TRUE)
#> <ParamSet(4)>
#> id class lower upper nlevels default value
#> <char> <char> <num> <num> <num> <list> <list>
#> 1: i.first ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 2: z.first ParamLgl NA NA 2 <NoDefault[0]> [NULL]
#> 3: i.last ParamInt -Inf Inf Inf <NoDefault[0]> [NULL]
#> 4: z.last ParamLgl NA NA 2 <NoDefault[0]> [NULL]
pset$values = list(i = 1, z = FALSE)
psr = ps_replicate(pset, 2, tag_sets = TRUE, tag_params = TRUE)
# observe the effect of tag_sets, tag_params:
psr$tags
#> $rep1.i
#> [1] "set_rep1" "param_i"
#>
#> $rep1.z
#> [1] "set_rep1" "param_z"
#>
#> $rep2.i
#> [1] "set_rep2" "param_i"
#>
#> $rep2.z
#> [1] "set_rep2" "param_z"
#>
# note that values are repeated as well
psr$values
#> $rep1.i
#> [1] 1
#>
#> $rep1.z
#> [1] FALSE
#>
#> $rep2.i
#> [1] 1
#>
#> $rep2.z
#> [1] FALSE
#>
psr$set_values(rep1.i = 10, rep2.z = TRUE)
psr$values
#> $rep1.i
#> [1] 10
#>
#> $rep1.z
#> [1] FALSE
#>
#> $rep2.i
#> [1] 1
#>
#> $rep2.z
#> [1] TRUE
#>
# use `any_tags` to get subset of values.
# `any_tags = ` is preferable to `tags = `, since parameters
# could also have other tags. `tags = ` would require the
# selected params to have the given tags exclusively.
# get all values associated with the original parameter `i`
psr$get_values(any_tags = "param_i")
#> $rep1.i
#> [1] 10
#>
#> $rep2.i
#> [1] 1
#>
# get all values associated with the first repetition "rep1"
psr$get_values(any_tags = "set_rep1")
#> $rep1.i
#> [1] 10
#>
#> $rep1.z
#> [1] FALSE
#>