1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#' @title Return ids of parameters in parameter set.
#'
#' @description
#' Useful if vectors are included.
#'
#' @template arg_par_or_set
#' @param repeated (`logical(1)`)\cr
#' Should ids be repeated length-times if parameter is a vector?
#' Default is `FALSE`.
#' @param with.nr (`logical(1)`)\cr
#' Should number from 1 to length be appended to id if `repeated` is `TRUE`?
#' Otherwise ignored.
#' Default is `FALSE`.
#' @return [`character`].
#' @export
#' @examples
#' ps = makeParamSet(
#' makeNumericParam("u"),
#' makeIntegerVectorParam("v", len = 2)
#' )
#' getParamIds(ps)
#' getParamIds(ps, repeated = TRUE)
#' getParamIds(ps, repeated = TRUE, with.nr = TRUE)
getParamIds = function(par, repeated = FALSE, with.nr = FALSE) {
assertFlag(repeated)
assertFlag(with.nr)
UseMethod("getParamIds")
}
#' @export
getParamIds.ParamSet = function(par, repeated = FALSE, with.nr = FALSE) {
if (isEmpty(par)) {
return(character(0L))
}
unlist(lapply(par$pars, getParamIds, repeated = repeated, with.nr = with.nr), use.names = FALSE)
}
#' @export
getParamIds.Param = function(par, repeated = FALSE, with.nr = FALSE) {
pid = par$id
if (repeated && isVector(par)) {
n = par$len
if (!is.na(n)) {
if (n > 1L && with.nr) {
paste(rep(pid, n), seq_len(n), sep = "")
} else {
rep(pid, n)
}
} else {
pid
}
} else {
pid
}
}
|