|
| 1 | +#' A placeholder function for argument values |
| 2 | +#' |
| 3 | +#' [varying()] is used when a parameter will be specified at a later date. |
| 4 | +#' @export |
| 5 | +varying <- function() |
| 6 | + quote(varying()) |
| 7 | + |
| 8 | +#' Determine varying arguments |
| 9 | +#' |
| 10 | +#' `varying_args` takes a model specification and lists all of the arguments |
| 11 | +#' along with whether they are fully specified or not. |
| 12 | +#' @param x An object |
| 13 | +#' @param id A string describing the object `x`. |
| 14 | +#' @param ... Not currently used. |
| 15 | +#' @return A tibble with columns for the parameter name (`name`), whether is |
| 16 | +#' contains _any_ varying value (`varying`), the `id` for the object, and the |
| 17 | +#' class that was used to call the method (`type`). |
| 18 | +#' @examples |
| 19 | +#' library(dplyr) |
| 20 | +#' library(rlang) |
| 21 | +#' |
| 22 | +#' rand_forest() %>% varying_args(id = "plain") |
| 23 | +#' |
| 24 | +#' rand_forest(mtry = varying()) %>% varying_args(id = "one arg") |
| 25 | +#' |
| 26 | +#' rand_forest(others = list(sample.fraction = varying())) %>% |
| 27 | +#' varying_args(id = "only others") |
| 28 | +#' |
| 29 | +#' rand_forest( |
| 30 | +#' others = list( |
| 31 | +#' strata = expr(Class), |
| 32 | +#' sampsize = c(varying(), varying()) |
| 33 | +#' ) |
| 34 | +#' ) %>% |
| 35 | +#' varying_args(id = "add an expr") |
| 36 | +#' |
| 37 | +#' rand_forest(others = list(classwt = c(class1 = 1, class2 = varying()))) %>% |
| 38 | +#' varying_args(id = "list of values") |
| 39 | +#' |
| 40 | +#' @export |
| 41 | +varying_args <- function (x, id, ...) |
| 42 | + UseMethod("varying_args") |
| 43 | + |
| 44 | +#' @importFrom purrr map map_lgl |
| 45 | +#' @export |
| 46 | +#' @export varying_args.model_spec |
| 47 | +#' @rdname varying_args |
| 48 | +varying_args.model_spec <- function(x, id = NULL, ...) { |
| 49 | + cl <- match.call() |
| 50 | + |
| 51 | + if (!is.null(id) && !is.character(id)) |
| 52 | + stop ("`id` should be a single character string.", call. = FALSE) |
| 53 | + id <- id[1] |
| 54 | + |
| 55 | + if (is.null(id)) |
| 56 | + id <- deparse(cl$x) |
| 57 | + varying_args <- map(x$args, find_varying) |
| 58 | + varying_others <- map(x$others, find_varying) |
| 59 | + res <- c(varying_args, varying_others) |
| 60 | + res <- map_lgl(res, any) |
| 61 | + tibble( |
| 62 | + name = names(res), |
| 63 | + varying = unname(res), |
| 64 | + id = id, |
| 65 | + type = caller_method(cl) |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +# NOTE Look at the `sampsize` and `classwt` examples above. Using varying() in |
| 70 | +# a vector will convert it to list. When the model-specific `translate` is |
| 71 | +# run, we should catch those and convert them back to vectors if the varying |
| 72 | +# parameter has been replaced with a real value. |
| 73 | + |
| 74 | +# Need to figure out a way to meld the results of varying_args with |
| 75 | +# parameter objects from `dials` or from novel parameters in the user's |
| 76 | +# workspace. Maybe register the parameters in dials and have a way of |
| 77 | +# adding/modifying them? A list vector could be added to these tibbles with |
| 78 | +# the actual parameter objects (and the ranges may need to be set). |
| 79 | + |
| 80 | +# Maybe use this data as substrate to make a new object type (param_set?) that |
| 81 | +# would have its own methods for grids and random sampling. |
| 82 | + |
| 83 | +# lots of code duplication below and probably poor planning; just a prototype. |
| 84 | +# once the generics package is done, these will go into recipes |
| 85 | + |
| 86 | +#' @importFrom purrr map2_dfr map_chr |
| 87 | +#' @export |
| 88 | +#' @export varying_args.recipe |
| 89 | +#' @rdname varying_args |
| 90 | +varying_args.recipe <- function(x, id = NULL, ...) { |
| 91 | + step_type <- map_chr(x$steps, function(x) class(x)[1]) |
| 92 | + step_type <- make.names(step_type, unique = TRUE) # change with new tibble version |
| 93 | + res <- map2_dfr(x$steps, step_type, varying_args) |
| 94 | + res |
| 95 | +} |
| 96 | + |
| 97 | +#' @importFrom purrr map map_lgl |
| 98 | +#' @export |
| 99 | +#' @export varying_args.step |
| 100 | +#' @rdname varying_args |
| 101 | +varying_args.step <- function(x, id = NULL, ...) { |
| 102 | + cl <- match.call() |
| 103 | + if (!is.null(id) && !is.character(id)) |
| 104 | + stop ("`id` should be a single character string.", call. = FALSE) |
| 105 | + id <- id[1] |
| 106 | + |
| 107 | + if (is.null(id)) |
| 108 | + id <- deparse(cl$x) |
| 109 | + |
| 110 | + exclude <- |
| 111 | + c("terms", "role", "trained", "skip", "na.rm", "impute_with", "seed", |
| 112 | + "prefix", "naming", "denom", "outcome", "id") |
| 113 | + x <- x[!(names(x) %in% exclude)] |
| 114 | + x <- x[!map_lgl(x, is.null)] |
| 115 | + res <- map(x, find_varying) |
| 116 | + res <- map_lgl(res, any) |
| 117 | + tibble( |
| 118 | + name = names(res), |
| 119 | + varying = unname(res), |
| 120 | + id = id, |
| 121 | + type = caller_method(cl) |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +# helpers ---------------------------------------------------------------------- |
| 126 | + |
| 127 | +is_varying <- function(x) { |
| 128 | + if(is.null(x)) { |
| 129 | + res <- FALSE |
| 130 | + } else { |
| 131 | + res <- if(is_quosure(x)) |
| 132 | + isTRUE(all.equal(x[[-1]], quote(varying()))) |
| 133 | + else |
| 134 | + isTRUE(all.equal(x, quote(varying()))) |
| 135 | + } |
| 136 | + res |
| 137 | +} |
| 138 | + |
| 139 | +# Error: C stack usage 7970880 is too close to the limit (in some cases) |
| 140 | +find_varying <- function(x) { |
| 141 | + if (is.atomic(x) | is.name(x)) { |
| 142 | + FALSE |
| 143 | + } else if (is.call(x)) { |
| 144 | + if (is_varying(x)) { |
| 145 | + TRUE |
| 146 | + } else { |
| 147 | + find_varying(x) |
| 148 | + } |
| 149 | + } else if (is.pairlist(x)) { |
| 150 | + find_varying(x) |
| 151 | + } else if (is.vector(x) | is.list(x)) { |
| 152 | + map_lgl(x, find_varying) |
| 153 | + } else { |
| 154 | + # User supplied incorrect input |
| 155 | + stop("Don't know how to handle type ", typeof(x), |
| 156 | + call. = FALSE) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +caller_method <- function(cl) { |
| 161 | + x <- cl[[1]] |
| 162 | + x <- deparse(x) |
| 163 | + x <- gsub("varying_args.", "", x, fixed = TRUE) |
| 164 | + x |
| 165 | +} |
| 166 | + |
0 commit comments