File: getParamIds.R

package info (click to toggle)
r-cran-paramhelpers 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 992 kB
  • sloc: ansic: 102; sh: 13; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,410 bytes parent folder | download | duplicates (3)
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
  }
}