File: getParamTypes.R

package info (click to toggle)
r-cran-paramhelpers 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 984 kB
  • sloc: ansic: 260; sh: 13; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download
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
56
57
58
#' Returns type information for a parameter set.
#'
#' @template arg_parset
#' @param df.cols [\code{logical(1)}]\cr
#'   If \code{FALSE} simply return the parameter types in the set,
#'   i.e., \code{par$type}.
#'   If \code{TRUE}, convert types so they correspond to R types of a data.frame
#'   where values of this set might be stored.
#'   This also results in replication of output types for
#'   vector parameters.
#'   Default is \code{FALSE}.
#' @param df.discretes.as.factor [\code{logical(1)}]\cr
#'   If \code{df.cols} is \code{TRUE}:
#'   Should type for discrete params be \code{factor} or \code{character}?
#'   Default is \code{TRUE}.
#' @param use.names [\code{logical(1)}]\cr
#'   Name the result vector?
#'   Default is \code{FALSE}.
#' @param with.nr [\code{logical(1)}]\cr
#'   Should number from 1 to length be appended to name?
#'   Only used if \code{use.names} and \code{df.cols} are \code{TRUE}.
#'   Default is \code{TRUE}.
#' @return [\code{character}].
#' @export
getParamTypes = function(par.set, df.cols = FALSE, df.discretes.as.factor = TRUE,
  use.names = FALSE, with.nr = TRUE) {
  assertClass(par.set, "ParamSet")
  assertFlag(df.cols)
  assertFlag(df.discretes.as.factor)
  assertFlag(use.names)
  assertFlag(with.nr)

  types = extractSubList(par.set$pars, "type")
  if (length(types) == 0L)
    return(character(0L))

  recode = function(types, from, to) {
    i = fmatch(types, from, nomatch = 0L)
    types[i > 0L] = to[i]
    rep(types, getParamLengths(par.set))
  }


  if (df.cols) {
    to = if (df.discretes.as.factor) {
      c("numeric", "integer", "factor", "factor", "logical", "character")
    } else {
      c("numeric", "integer",  "character", "character", "logical","character")
    }
    types = recode(types, ph$convert.param.types.from, to)
  }

  ns = if (use.names)
    getParamIds(par.set, repeated = df.cols, with.nr = with.nr)
  else
    NULL
  return(setNames(types, ns))
}