Skip to content

Commit 1bd377c

Browse files
authored
Merge pull request #91 from topepo/quosure-passthrough-max
quosure updates
2 parents 53becc5 + 20d6364 commit 1bd377c

21 files changed

+414
-390
lines changed

R/arguments.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ check_others <- function(args, obj, core_args) {
8686
#'
8787
#' @export
8888
set_args <- function(object, ...) {
89-
the_dots <- list(...)
89+
the_dots <- enquos(...)
9090
if (length(the_dots) == 0)
9191
stop("Please pass at least one named argument.", call. = FALSE)
9292
main_args <- names(object$args)

R/boost_tree.R

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@
2222
#' }
2323
#' These arguments are converted to their specific names at the
2424
#' time that the model is fit. Other options and argument can be
25-
#' set using the `others` argument. If left to their defaults
25+
#' set using the `...` slot. If left to their defaults
2626
#' here (`NULL`), the values are taken from the underlying model
2727
#' functions. If parameters need to be modified, `update` can be used
2828
#' in lieu of recreating the object from scratch.
2929
#'
3030
#' @param mode A single character string for the type of model.
3131
#' Possible values for this model are "unknown", "regression", or
3232
#' "classification".
33-
#' @param others A named list of arguments to be used by the
34-
#' underlying models (e.g., `xgboost::xgb.train`, etc.). .
3533
#' @param mtry An number for the number (or proportion) of predictors that will
3634
#' be randomly sampled at each split when creating the tree models (`xgboost`
3735
#' only).
@@ -48,8 +46,11 @@
4846
#' @param sample_size An number for the number (or proportion) of data that is
4947
#' exposed to the fitting routine. For `xgboost`, the sampling is done at at
5048
#' each iteration while `C5.0` samples once during traning.
51-
#' @param ... Used for method consistency. Any arguments passed to
52-
#' the ellipses will result in an error. Use `others` instead.
49+
#' @param ... Other arguments to pass to the specific engine's
50+
#' model fit function (see the Engine Details section below). This
51+
#' should not include arguments defined by the main parameters to
52+
#' this function. For the `update` function, the ellipses can
53+
#' contain the primary arguments or any others.
5354
#' @details
5455
#' The data given to the function are not saved and are only used
5556
#' to determine the _mode_ of the model. For `boost_tree`, the
@@ -62,12 +63,15 @@
6263
#' \item \pkg{Spark}: `"spark"`
6364
#' }
6465
#'
65-
#' Main parameter arguments (and those in `others`) can avoid
66+
#' Main parameter arguments (and those in `...`) can avoid
6667
#' evaluation until the underlying function is executed by wrapping the
6768
#' argument in [rlang::expr()] (e.g. `mtry = expr(floor(sqrt(p)))`).
6869
#'
70+
#'
71+
#' @section Engine Details:
72+
#'
6973
#' Engines may have pre-set default arguments when executing the
70-
#' model fit call. These can be changed by using the `others`
74+
#' model fit call. These can be changed by using the `...`
7175
#' argument to pass in the preferred values. For this type of
7276
#' model, the template of the fit calls are:
7377
#'
@@ -114,13 +118,18 @@
114118

115119
boost_tree <-
116120
function(mode = "unknown",
117-
...,
118121
mtry = NULL, trees = NULL, min_n = NULL,
119122
tree_depth = NULL, learn_rate = NULL,
120123
loss_reduction = NULL,
121124
sample_size = NULL,
122-
others = list()) {
123-
check_empty_ellipse(...)
125+
...) {
126+
others <- enquos(...)
127+
mtry <- enquo(mtry)
128+
trees <- enquo(trees)
129+
min_n <- enquo(min_n)
130+
learn_rate <- enquo(learn_rate)
131+
loss_reduction <- enquo(loss_reduction)
132+
sample_size <- enquo(sample_size)
124133

125134
if (!(mode %in% boost_tree_modes))
126135
stop("`mode` should be one of: ",
@@ -184,10 +193,15 @@ update.boost_tree <-
184193
mtry = NULL, trees = NULL, min_n = NULL,
185194
tree_depth = NULL, learn_rate = NULL,
186195
loss_reduction = NULL, sample_size = NULL,
187-
others = list(),
188196
fresh = FALSE,
189197
...) {
190-
check_empty_ellipse(...)
198+
others <- enquos(...)
199+
mtry <- enquo(mtry)
200+
trees <- enquo(trees)
201+
min_n <- enquo(min_n)
202+
learn_rate <- enquo(learn_rate)
203+
loss_reduction <- enquo(loss_reduction)
204+
sample_size <- enquo(sample_size)
191205

192206
args <- list(
193207
mtry = mtry, trees = trees, min_n = min_n, tree_depth = tree_depth,

R/linear_reg.R

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,19 @@
1212
#' }
1313
#' These arguments are converted to their specific names at the
1414
#' time that the model is fit. Other options and argument can be
15-
#' set using the `others` argument. If left to their defaults
15+
#' set using the `...` slot. If left to their defaults
1616
#' here (`NULL`), the values are taken from the underlying model
1717
#' functions. If parameters need to be modified, `update` can be used
1818
#' in lieu of recreating the object from scratch.
19+
#' @inheritParams boost_tree
1920
#' @param mode A single character string for the type of model.
2021
#' The only possible value for this model is "regression".
21-
#' @param others A named list of arguments to be used by the
22-
#' underlying models (e.g., `stats::lm`,
23-
#' `rstanarm::stan_glm`, etc.). These are not evaluated
24-
#' until the model is fit and will be substituted into the model
25-
#' fit expression.
2622
#' @param penalty An non-negative number representing the
2723
#' total amount of regularization (`glmnet` and `spark` only).
2824
#' @param mixture A number between zero and one (inclusive) that
2925
#' represents the proportion of regularization that is used for the
3026
#' L2 penalty (i.e. weight decay, or ridge regression) versus L1
3127
#' (the lasso) (`glmnet` and `spark` only).
32-
#' @param ... Used for S3 method consistency. Any arguments passed to
33-
#' the ellipses will result in an error. Use `others` instead.
3428
#'
3529
#' @details
3630
#' The data given to the function are not saved and are only used
@@ -45,8 +39,10 @@
4539
#' \item \pkg{Spark}: `"spark"`
4640
#' }
4741
#'
42+
#' @section Engine Details:
43+
#'
4844
#' Engines may have pre-set default arguments when executing the
49-
#' model fit call. These can be changed by using the `others`
45+
#' model fit call. These can be changed by using the `...`
5046
#' argument to pass in the preferred values. For this type of
5147
#' model, the template of the fit calls are:
5248
#'
@@ -105,11 +101,13 @@
105101
#' @importFrom purrr map_lgl
106102
linear_reg <-
107103
function(mode = "regression",
108-
...,
109104
penalty = NULL,
110105
mixture = NULL,
111-
others = list()) {
112-
check_empty_ellipse(...)
106+
...) {
107+
others <- enquos(...)
108+
penalty <- enquo(penalty)
109+
mixture <- enquo(mixture)
110+
113111
if (!(mode %in% linear_reg_modes))
114112
stop(
115113
"`mode` should be one of: ",
@@ -121,7 +119,7 @@ linear_reg <-
121119
stop("The amount of regularization should be >= 0", call. = FALSE)
122120
if (is.numeric(mixture) && (mixture < 0 | mixture > 1))
123121
stop("The mixture proportion should be within [0,1]", call. = FALSE)
124-
if (length(mixture) > 1)
122+
if (is.numeric(mixture) && length(mixture) > 1)
125123
stop("Only one value of `mixture` is allowed.", call. = FALSE)
126124

127125
args <- list(penalty = penalty, mixture = mixture)
@@ -156,11 +154,8 @@ print.linear_reg <- function(x, ...) {
156154

157155
# ------------------------------------------------------------------------------
158156

159-
#' @inheritParams linear_reg
157+
#' @inheritParams update.boost_tree
160158
#' @param object A linear regression model specification.
161-
#' @param fresh A logical for whether the arguments should be
162-
#' modified in-place of or replaced wholesale.
163-
#' @return An updated model specification.
164159
#' @examples
165160
#' model <- linear_reg(penalty = 10, mixture = 0.1)
166161
#' model
@@ -172,10 +167,11 @@ print.linear_reg <- function(x, ...) {
172167
update.linear_reg <-
173168
function(object,
174169
penalty = NULL, mixture = NULL,
175-
others = list(),
176170
fresh = FALSE,
177171
...) {
178-
check_empty_ellipse(...)
172+
others <- enquos(...)
173+
penalty <- enquo(penalty)
174+
mixture <- enquo(mixture)
179175

180176
if (is.numeric(penalty) && penalty < 0)
181177
stop("The amount of regularization should be >= 0", call. = FALSE)

R/logistic_reg.R

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,19 @@
1212
#' }
1313
#' These arguments are converted to their specific names at the
1414
#' time that the model is fit. Other options and argument can be
15-
#' set using the `others` argument. If left to their defaults
15+
#' set using the `...` slot. If left to their defaults
1616
#' here (`NULL`), the values are taken from the underlying model
1717
#' functions. If parameters need to be modified, `update` can be used
1818
#' in lieu of recreating the object from scratch.
19+
#' @inheritParams boost_tree
1920
#' @param mode A single character string for the type of model.
2021
#' The only possible value for this model is "classification".
21-
#' @param others A named list of arguments to be used by the
22-
#' underlying models (e.g., `stats::glm`,
23-
#' `rstanarm::stan_glm`, etc.). These are not evaluated
24-
#' until the model is fit and will be substituted into the model
25-
#' fit expression.
2622
#' @param penalty An non-negative number representing the
2723
#' total amount of regularization (`glmnet` and `spark` only).
2824
#' @param mixture A number between zero and one (inclusive) that
2925
#' represents the proportion of regularization that is used for the
3026
#' L2 penalty (i.e. weight decay, or ridge regression) versus L1
3127
#' (the lasso) (`glmnet` and `spark` only).
32-
#' @param ... Used for S3 method consistency. Any arguments passed to
33-
#' the ellipses will result in an error. Use `others` instead.
3428
#' @details
3529
#' For `logistic_reg`, the mode will always be "classification".
3630
#'
@@ -42,8 +36,10 @@
4236
#' \item \pkg{Spark}: `"spark"`
4337
#' }
4438
#'
39+
#' @section Engine Details:
40+
#'
4541
#' Engines may have pre-set default arguments when executing the
46-
#' model fit call. These can be changed by using the `others`
42+
#' model fit call. These can be changed by using the `...`
4743
#' argument to pass in the preferred values. For this type of
4844
#' model, the template of the fit calls are:
4945
#'
@@ -103,11 +99,13 @@
10399
#' @importFrom purrr map_lgl
104100
logistic_reg <-
105101
function(mode = "classification",
106-
...,
107102
penalty = NULL,
108103
mixture = NULL,
109-
others = list()) {
110-
check_empty_ellipse(...)
104+
...) {
105+
others <- enquos(...)
106+
penalty <- enquo(penalty)
107+
mixture <- enquo(mixture)
108+
111109
if (!(mode %in% logistic_reg_modes))
112110
stop(
113111
"`mode` should be one of: ",
@@ -152,11 +150,8 @@ print.logistic_reg <- function(x, ...) {
152150

153151
# ------------------------------------------------------------------------------
154152

155-
#' @inheritParams logistic_reg
153+
#' @inheritParams update.boost_tree
156154
#' @param object A logistic regression model specification.
157-
#' @param fresh A logical for whether the arguments should be
158-
#' modified in-place of or replaced wholesale.
159-
#' @return An updated model specification.
160155
#' @examples
161156
#' model <- logistic_reg(penalty = 10, mixture = 0.1)
162157
#' model
@@ -168,10 +163,11 @@ print.logistic_reg <- function(x, ...) {
168163
update.logistic_reg <-
169164
function(object,
170165
penalty = NULL, mixture = NULL,
171-
others = list(),
172166
fresh = FALSE,
173167
...) {
174-
check_empty_ellipse(...)
168+
others <- enquos(...)
169+
penalty <- enquo(penalty)
170+
mixture <- enquo(mixture)
175171

176172
if (is.numeric(penalty) && penalty < 0)
177173
stop("The amount of regularization should be >= 0", call. = FALSE)

R/logistic_reg_data.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ logistic_reg_glm_data <-
3030
func = c(pkg = "stats", fun = "glm"),
3131
defaults =
3232
list(
33-
family = expr(binomial)
33+
family = expr(stats::binomial)
3434
)
3535
),
3636
classes = list(
@@ -151,7 +151,7 @@ logistic_reg_stan_data <-
151151
func = c(pkg = "rstanarm", fun = "stan_glm"),
152152
defaults =
153153
list(
154-
family = expr(binomial)
154+
family = expr(stats::binomial)
155155
)
156156
),
157157
classes = list(

R/mars.R

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,20 @@
1717
#' }
1818
#' These arguments are converted to their specific names at the
1919
#' time that the model is fit. Other options and argument can be
20-
#' set using the `others` argument. If left to their defaults
20+
#' set using the `...` slot. If left to their defaults
2121
#' here (`NULL`), the values are taken from the underlying model
2222
#' functions. If parameters need to be modified, `update` can be used
2323
#' in lieu of recreating the object from scratch.
2424
#'
25+
#' @inheritParams boost_tree
2526
#' @param mode A single character string for the type of model.
2627
#' Possible values for this model are "unknown", "regression", or
2728
#' "classification".
28-
#' @param others A named list of arguments to be used by the
29-
#' underlying models (e.g., `earth::earth`, etc.). If the outcome is a factor
30-
#' and `mode = "classification"`, `others` can include the `glm` argument to
31-
#' `earth::earth`. If this argument is not passed, it will be added prior to
32-
#' the fitting occurs.
3329
#' @param num_terms The number of features that will be retained in the
3430
#' final model, including the intercept.
3531
#' @param prod_degree The highest possible interaction degree.
3632
#' @param prune_method The pruning method.
37-
#' @param ... Used for method consistency. Any arguments passed to
38-
#' the ellipses will result in an error. Use `others` instead.
39-
#' @details Main parameter arguments (and those in `others`) can avoid
33+
#' @details Main parameter arguments (and those in `...`) can avoid
4034
#' evaluation until the underlying function is executed by wrapping the
4135
#' argument in [rlang::expr()].
4236
#'
@@ -46,8 +40,10 @@
4640
#' \item \pkg{R}: `"earth"`
4741
#' }
4842
#'
43+
#' @section Engine Details:
44+
#'
4945
#' Engines may have pre-set default arguments when executing the
50-
#' model fit call. These can be changed by using the `others`
46+
#' model fit call. These can be changed by using the `...`
5147
#' argument to pass in the preferred values. For this type of
5248
#' model, the template of the fit calls are:
5349
#'
@@ -71,10 +67,12 @@
7167

7268
mars <-
7369
function(mode = "unknown",
74-
...,
7570
num_terms = NULL, prod_degree = NULL, prune_method = NULL,
76-
others = list()) {
77-
check_empty_ellipse(...)
71+
...) {
72+
others <- enquos(...)
73+
num_terms <- enquo(num_terms)
74+
prod_degree <- enquo(prod_degree)
75+
prune_method <- enquo(prune_method)
7876

7977
if (!(mode %in% mars_modes))
8078
stop("`mode` should be one of: ",
@@ -87,7 +85,7 @@ mars <-
8785
stop("`num_terms` should be >= 1", call. = FALSE)
8886
if (!is_varying(prune_method) &&
8987
!is.null(prune_method) &&
90-
!is.character(prune_method))
88+
is.character(prune_method))
9189
stop("`prune_method` should be a single string value", call. = FALSE)
9290

9391
args <- list(num_terms = num_terms,
@@ -118,11 +116,8 @@ print.mars <- function(x, ...) {
118116
# ------------------------------------------------------------------------------
119117

120118
#' @export
121-
#' @inheritParams mars
119+
#' @inheritParams update.boost_tree
122120
#' @param object A MARS model specification.
123-
#' @param fresh A logical for whether the arguments should be
124-
#' modified in-place of or replaced wholesale.
125-
#' @return An updated model specification.
126121
#' @examples
127122
#' model <- mars(num_terms = 10, prune_method = "none")
128123
#' model
@@ -134,10 +129,12 @@ print.mars <- function(x, ...) {
134129
update.mars <-
135130
function(object,
136131
num_terms = NULL, prod_degree = NULL, prune_method = NULL,
137-
others = list(),
138132
fresh = FALSE,
139133
...) {
140-
check_empty_ellipse(...)
134+
others <- enquos(...)
135+
num_terms <- enquo(num_terms)
136+
prod_degree <- enquo(prod_degree)
137+
prune_method <- enquo(prune_method)
141138

142139
args <- list(num_terms = num_terms,
143140
prod_degree = prod_degree,

0 commit comments

Comments
 (0)