Skip to content

Commit 9e36249

Browse files
authored
code style in checking internals (#778)
1 parent 47e708b commit 9e36249

File tree

4 files changed

+59
-38
lines changed

4 files changed

+59
-38
lines changed

R/misc.R

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ make_classes <- function(prefix) {
1515
#' @return If an error is not thrown (from non-empty ellipses), a NULL list.
1616
#' @keywords internal
1717
#' @export
18-
check_empty_ellipse <- function (...) {
18+
check_empty_ellipse <- function(...) {
1919
terms <- quos(...)
20-
if (!is_empty(terms))
20+
if (!is_empty(terms)) {
2121
rlang::abort("Please pass other arguments to the model function via `set_engine()`.")
22+
}
2223
terms
2324
}
2425

25-
is_missing_arg <- function(x)
26+
is_missing_arg <- function(x) {
2627
identical(x, quote(missing_arg()))
28+
}
2729

2830
model_info_table <-
2931
utils::read.delim(system.file("models.tsv", package = "parsnip"))
@@ -38,7 +40,11 @@ has_loaded_implementation <- function(spec_, engine_, mode_) {
3840
if (isFALSE(mode_ %in% c("regression", "censored regression", "classification"))) {
3941
mode_ <- c("regression", "censored regression", "classification")
4042
}
41-
eng_cond <- if (is.null(engine_)) {TRUE} else {quote(engine == engine_)}
43+
eng_cond <- if (is.null(engine_)) {
44+
TRUE
45+
} else {
46+
quote(engine == engine_)
47+
}
4248

4349
avail <-
4450
get_from_env(spec_) %>%
@@ -56,7 +62,7 @@ has_loaded_implementation <- function(spec_, engine_, mode_) {
5662

5763
is_printable_spec <- function(x) {
5864
!is.null(x$method$fit$args) &&
59-
has_loaded_implementation(class(x)[1], x$engine, x$mode)
65+
has_loaded_implementation(class(x)[1], x$engine, x$mode)
6066
}
6167

6268
# construct a message informing the user that there are no
@@ -109,22 +115,25 @@ show_call <- function(object) {
109115
map(object$method$fit$args, convert_arg)
110116

111117
call2(object$method$fit$func["fun"],
112-
!!!object$method$fit$args,
113-
.ns = object$method$fit$func["pkg"])
118+
!!!object$method$fit$args,
119+
.ns = object$method$fit$func["pkg"]
120+
)
114121
}
115122

116123
convert_arg <- function(x) {
117-
if (is_quosure(x))
124+
if (is_quosure(x)) {
118125
quo_get_expr(x)
119-
else
126+
} else {
120127
x
128+
}
121129
}
122130

123131
levels_from_formula <- function(f, dat) {
124-
if (inherits(dat, "tbl_spark"))
132+
if (inherits(dat, "tbl_spark")) {
125133
res <- NULL
126-
else
134+
} else {
127135
res <- levels(eval_tidy(f[[2]], dat))
136+
}
128137
res
129138
}
130139

@@ -134,7 +143,7 @@ levels_from_formula <- function(f, dat) {
134143
show_fit <- function(model, eng) {
135144
mod <- translate(x = model, engine = eng)
136145
fit_call <- show_call(mod)
137-
call_text <- deparse(fit_call)
146+
call_text <- deparse(fit_call)
138147
call_text <- paste0(call_text, collapse = "\n")
139148
paste0(
140149
"\\preformatted{\n",
@@ -157,9 +166,10 @@ check_args.default <- function(object) {
157166

158167
# copied form recipes
159168

160-
names0 <- function (num, prefix = "x") {
161-
if (num < 1)
169+
names0 <- function(num, prefix = "x") {
170+
if (num < 1) {
162171
rlang::abort("`num` should be > 0.")
172+
}
163173
ind <- format(1:num)
164174
ind <- gsub(" ", "0", ind)
165175
paste0(prefix, ind)
@@ -172,16 +182,16 @@ names0 <- function (num, prefix = "x") {
172182
#' @keywords internal
173183
#' @rdname add_on_exports
174184
update_dot_check <- function(...) {
175-
176185
dots <- enquos(...)
177186

178-
if (length(dots) > 0)
187+
if (length(dots) > 0) {
179188
rlang::abort(
180189
glue::glue(
181190
"Extra arguments will be ignored: ",
182191
glue::glue_collapse(glue::glue("`{names(dots)}`"), sep = ", ")
183192
)
184193
)
194+
}
185195
invisible(NULL)
186196
}
187197

@@ -192,15 +202,16 @@ update_dot_check <- function(...) {
192202
#' @rdname add_on_exports
193203
new_model_spec <- function(cls, args, eng_args, mode, method, engine,
194204
check_missing_spec = TRUE) {
195-
196205
check_spec_mode_engine_val(cls, engine, mode)
197206

198207
if ((!has_loaded_implementation(cls, engine, mode)) && check_missing_spec) {
199208
rlang::inform(inform_missing_implementation(cls, engine, mode))
200209
}
201210

202-
out <- list(args = args, eng_args = eng_args,
203-
mode = mode, method = method, engine = engine)
211+
out <- list(
212+
args = args, eng_args = eng_args,
213+
mode = mode, method = method, engine = engine
214+
)
204215
class(out) <- make_classes(cls)
205216
out
206217
}
@@ -211,8 +222,9 @@ check_outcome <- function(y, spec) {
211222
if (spec$mode == "unknown") {
212223
return(invisible(NULL))
213224
} else if (spec$mode == "regression") {
214-
if (!all(map_lgl(y, is.numeric)))
225+
if (!all(map_lgl(y, is.numeric))) {
215226
rlang::abort("For a regression model, the outcome should be numeric.")
227+
}
216228
} else if (spec$mode == "classification") {
217229
if (!all(map_lgl(y, is.factor))) {
218230
rlang::abort("For a classification model, the outcome should be a factor.")
@@ -250,7 +262,6 @@ check_final_param <- function(x) {
250262
#' @keywords internal
251263
#' @rdname add_on_exports
252264
update_main_parameters <- function(args, param) {
253-
254265
if (length(param) == 0) {
255266
return(args)
256267
}
@@ -263,8 +274,10 @@ update_main_parameters <- function(args, param) {
263274
extra_args <- names(param)[has_extra_args]
264275
if (any(has_extra_args)) {
265276
rlang::abort(
266-
paste("At least one argument is not a main argument:",
267-
paste0("`", extra_args, "`", collapse = ", "))
277+
paste(
278+
"At least one argument is not a main argument:",
279+
paste0("`", extra_args, "`", collapse = ", ")
280+
)
268281
)
269282
}
270283
param <- param[!has_extra_args]
@@ -276,7 +289,6 @@ update_main_parameters <- function(args, param) {
276289
#' @keywords internal
277290
#' @rdname add_on_exports
278291
update_engine_parameters <- function(eng_args, fresh, ...) {
279-
280292
dots <- enquos(...)
281293

282294
## only update from dots when there are eng args in original model spec
@@ -303,16 +315,20 @@ update_engine_parameters <- function(eng_args, fresh, ...) {
303315
stan_conf_int <- function(object, newdata) {
304316
check_installs(list(method = list(libs = "rstanarm")))
305317
if (utils::packageVersion("rstanarm") >= "2.21.1") {
306-
fn <- rlang::call2("posterior_epred", .ns = "rstanarm",
307-
object = expr(object),
308-
newdata = expr(newdata),
309-
seed = expr(sample.int(10^5, 1)))
318+
fn <- rlang::call2("posterior_epred",
319+
.ns = "rstanarm",
320+
object = expr(object),
321+
newdata = expr(newdata),
322+
seed = expr(sample.int(10^5, 1))
323+
)
310324
} else {
311-
fn <- rlang::call2("posterior_linpred", .ns = "rstanarm",
312-
object = expr(object),
313-
newdata = expr(newdata),
314-
transform = TRUE,
315-
seed = expr(sample.int(10^5, 1)))
325+
fn <- rlang::call2("posterior_linpred",
326+
.ns = "rstanarm",
327+
object = expr(object),
328+
newdata = expr(newdata),
329+
transform = TRUE,
330+
seed = expr(sample.int(10^5, 1))
331+
)
316332
}
317333
rlang::eval_tidy(fn)
318334
}
@@ -357,30 +373,31 @@ stan_conf_int <- function(object, newdata) {
357373
#' @keywords internal
358374
#' @export
359375
.check_glmnet_penalty_predict <- function(penalty = NULL, object, multi = FALSE) {
360-
361376
if (is.null(penalty)) {
362377
penalty <- object$fit$lambda
363378
}
364379

365380
# when using `predict()`, allow for a single lambda
366381
if (!multi) {
367-
if (length(penalty) != 1)
382+
if (length(penalty) != 1) {
368383
rlang::abort(
369384
glue::glue(
370385
"`penalty` should be a single numeric value. `multi_predict()` ",
371386
"can be used to get multiple predictions per row of data.",
372387
)
373388
)
389+
}
374390
}
375391

376-
if (length(object$fit$lambda) == 1 && penalty != object$fit$lambda)
392+
if (length(object$fit$lambda) == 1 && penalty != object$fit$lambda) {
377393
rlang::abort(
378394
glue::glue(
379395
"The glmnet model was fit with a single penalty value of ",
380396
"{object$fit$lambda}. Predicting with a value of {penalty} ",
381397
"will give incorrect results from `glmnet()`."
382398
)
383399
)
400+
}
384401

385402
penalty
386403
}

R/print.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ print.model_spec <- function(x, ...) {
99
#' @rdname add_on_exports
1010
#' @export
1111
print_model_spec <- function(x, cls = class(x)[1], desc = get_model_desc(cls), ...) {
12+
1213
cat(desc, " Model Specification (", x$mode, ")\n\n", sep = "")
1314
model_printer(x, ...)
1415

R/required_pkgs.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ get_pkgs <- function(x, infra) {
4040
pkgs <-
4141
get_from_env(paste0(cls, "_pkgs")) %>%
4242
dplyr::filter(engine == x$engine)
43-
res <- pkgs$pkg[[1]]
43+
if (length(pkgs$pkg) == 0) {
44+
res <- character(0)
45+
} else {
46+
res <- pkgs$pkg[[1]]
47+
}
4448
if (length(res) == 0) {
4549
res <- character(0)
4650
}

parsnip.Rproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ StripTrailingWhitespace: Yes
1717

1818
BuildType: Package
1919
PackageUseDevtools: Yes
20-
PackageCleanBeforeInstall: Yes
2120
PackageInstallArgs: --no-multiarch --with-keep.source
2221
PackageRoxygenize: rd,collate,namespace

0 commit comments

Comments
 (0)