Skip to content

Commit da77668

Browse files
committed
code style
1 parent a3e08ee commit da77668

File tree

2 files changed

+80
-57
lines changed

2 files changed

+80
-57
lines changed

R/convert_data.R

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,38 @@
3838
#' @importFrom stats .checkMFClasses .getXlevels delete.response
3939
#' @importFrom stats model.offset model.weights na.omit na.pass
4040
.convert_form_to_xy_fit <- function(formula,
41-
data,
42-
...,
43-
na.action = na.omit,
44-
indicators = "traditional",
45-
composition = "data.frame",
46-
remove_intercept = TRUE) {
47-
48-
if (!(composition %in% c("data.frame", "matrix")))
41+
data,
42+
...,
43+
na.action = na.omit,
44+
indicators = "traditional",
45+
composition = "data.frame",
46+
remove_intercept = TRUE) {
47+
if (!(composition %in% c("data.frame", "matrix"))) {
4948
rlang::abort("`composition` should be either 'data.frame' or 'matrix'.")
49+
}
5050

5151
## Assemble model.frame call from call arguments
5252
mf_call <- quote(model.frame(formula, data))
5353
mf_call$na.action <- match.call()$na.action # TODO this should work better
5454
dots <- quos(...)
5555
check_form_dots(dots)
56-
for(i in seq_along(dots))
57-
mf_call[[ names(dots)[i] ]] <- get_expr(dots[[i]])
56+
for (i in seq_along(dots)) {
57+
mf_call[[names(dots)[i]]] <- get_expr(dots[[i]])
58+
}
5859

5960
# setup contrasts
60-
if (any(names(dots) == "contrasts"))
61+
if (any(names(dots) == "contrasts")) {
6162
contrasts <- eval_tidy(dots[["contrasts"]])
62-
else
63+
} else {
6364
contrasts <- NULL
65+
}
6466

6567
# For new data, save the expression to create offsets (if any)
66-
if (any(names(dots) == "offset"))
68+
if (any(names(dots) == "offset")) {
6769
offset_expr <- get_expr(dots[["offset"]])
68-
else
70+
} else {
6971
offset_expr <- NULL
72+
}
7073

7174
mod_frame <- eval_tidy(mf_call)
7275
mod_terms <- attr(mod_frame, "terms")
@@ -78,20 +81,22 @@
7881
y <- model.response(mod_frame, type = "any")
7982

8083
# if y is a numeric vector, model.response() added names
81-
if(is.atomic(y)) {
84+
if (is.atomic(y)) {
8285
names(y) <- NULL
8386
}
8487

8588
w <- as.vector(model.weights(mod_frame))
86-
if (!is.null(w) && !is.numeric(w))
89+
if (!is.null(w) && !is.numeric(w)) {
8790
rlang::abort("`weights` must be a numeric vector")
91+
}
8892

8993
offset <- as.vector(model.offset(mod_frame))
9094
if (!is.null(offset)) {
91-
if (length(offset) != nrow(mod_frame))
95+
if (length(offset) != nrow(mod_frame)) {
9296
rlang::abort(
9397
glue::glue("The offset data should have {nrow(mod_frame)} elements.")
94-
)
98+
)
99+
}
95100
}
96101

97102
if (indicators != "none") {
@@ -103,13 +108,13 @@
103108
options(contrasts = new_contr)
104109
}
105110
x <- model.matrix(mod_terms, mod_frame, contrasts)
106-
107111
} else {
108112
# this still ignores -vars in formula
109113
x <- model.frame(mod_terms, data)
110114
y_cols <- attr(mod_terms, "response")
111-
if (length(y_cols) > 0)
112-
x <- x[,-y_cols, drop = FALSE]
115+
if (length(y_cols) > 0) {
116+
x <- x[, -y_cols, drop = FALSE]
117+
}
113118
}
114119

115120
if (remove_intercept) {
@@ -124,8 +129,9 @@
124129
)
125130

126131
if (composition == "data.frame") {
127-
if (is.matrix(y))
132+
if (is.matrix(y)) {
128133
y <- as.data.frame(y)
134+
}
129135
res <-
130136
list(
131137
x = as.data.frame(x),
@@ -140,8 +146,9 @@
140146
} else {
141147
# Since a matrix is requested, try to convert y but check
142148
# to see if it is possible
143-
if (will_make_matrix(y))
149+
if (will_make_matrix(y)) {
144150
y <- as.matrix(y)
151+
}
145152
res <-
146153
list(
147154
x = x,
@@ -164,12 +171,12 @@
164171
#' @keywords internal
165172
#' @export
166173
.convert_form_to_xy_new <- function(object,
167-
new_data,
168-
na.action = na.pass,
169-
composition = "data.frame") {
170-
171-
if (!(composition %in% c("data.frame", "matrix")))
174+
new_data,
175+
na.action = na.pass,
176+
composition = "data.frame") {
177+
if (!(composition %in% c("data.frame", "matrix"))) {
172178
rlang::abort("`composition` should be either 'data.frame' or 'matrix'.")
179+
}
173180

174181
mod_terms <- object$terms
175182
mod_terms <- delete.response(mod_terms)
@@ -183,29 +190,38 @@
183190
# If offset was done at least once in-line
184191
if (!is.null(offset_cols)) {
185192
offset <- rep(0, nrow(new_data))
186-
for (i in offset_cols)
193+
for (i in offset_cols) {
187194
offset <- offset +
188-
eval_tidy(attr(mod_terms, "variables")[[i + 1]],
189-
new_data) # use na.action here and below?
190-
} else offset <- NULL
195+
eval_tidy(
196+
attr(mod_terms, "variables")[[i + 1]],
197+
new_data
198+
) # use na.action here and below?
199+
}
200+
} else {
201+
offset <- NULL
202+
}
191203

192204
if (!is.null(object$offset_expr)) {
193-
if (is.null(offset))
205+
if (is.null(offset)) {
194206
offset <- rep(0, nrow(new_data))
207+
}
195208
offset <- offset + eval_tidy(object$offset_expr, new_data)
196209
}
197210

198211
new_data <-
199-
model.frame(mod_terms,
200-
new_data,
201-
na.action = na.action,
202-
xlev = object$xlevels)
212+
model.frame(
213+
mod_terms,
214+
new_data,
215+
na.action = na.action,
216+
xlev = object$xlevels
217+
)
203218

204219
cl <- attr(mod_terms, "dataClasses")
205-
if (!is.null(cl))
220+
if (!is.null(cl)) {
206221
.checkMFClasses(cl, new_data)
222+
}
207223

208-
if(object$options$indicators != "none") {
224+
if (object$options$indicators != "none") {
209225
if (object$options$indicators == "one_hot") {
210226
old_contr <- options("contrasts")$contrasts
211227
on.exit(options(contrasts = old_contr))
@@ -217,15 +233,16 @@
217233
model.matrix(mod_terms, new_data, contrasts.arg = object$contrasts)
218234
}
219235

220-
if(object$options$remove_intercept) {
236+
if (object$options$remove_intercept) {
221237
new_data <- new_data[, colnames(new_data) != "(Intercept)", drop = FALSE]
222238
}
223239

224-
if (composition == "data.frame")
240+
if (composition == "data.frame") {
225241
new_data <- as.data.frame(new_data)
226-
else {
227-
if (will_make_matrix(new_data))
242+
} else {
243+
if (will_make_matrix(new_data)) {
228244
new_data <- as.matrix(new_data)
245+
}
229246
}
230247
list(x = new_data, offset = offset)
231248
}
@@ -247,21 +264,23 @@
247264
#'
248265
#' @importFrom dplyr bind_cols
249266
.convert_xy_to_form_fit <- function(x,
250-
y,
251-
weights = NULL,
252-
y_name = "..y",
253-
remove_intercept = TRUE) {
254-
if (is.vector(x))
267+
y,
268+
weights = NULL,
269+
y_name = "..y",
270+
remove_intercept = TRUE) {
271+
if (is.vector(x)) {
255272
rlang::abort("`x` cannot be a vector.")
273+
}
256274

257-
if(remove_intercept) {
275+
if (remove_intercept) {
258276
x <- x[, colnames(x) != "(Intercept)", drop = FALSE]
259277
}
260278

261279
rn <- rownames(x)
262280

263-
if (!is.data.frame(x))
281+
if (!is.data.frame(x)) {
264282
x <- as.data.frame(x)
283+
}
265284

266285
if (is.matrix(y)) {
267286
y <- as.data.frame(y)
@@ -277,14 +296,17 @@
277296
form <- make_formula(names(x), names(y))
278297

279298
x <- bind_cols(x, y)
280-
if(!is.null(rn) & !inherits(x, "tbl_df"))
299+
if (!is.null(rn) & !inherits(x, "tbl_df")) {
281300
rownames(x) <- rn
301+
}
282302

283303
if (!is.null(weights)) {
284-
if (!is.numeric(weights))
304+
if (!is.numeric(weights)) {
285305
rlang::abort("`weights` must be a numeric vector")
286-
if (length(weights) != nrow(x))
306+
}
307+
if (length(weights) != nrow(x)) {
287308
rlang::abort(glue::glue("`weights` should have {nrow(x)} elements"))
309+
}
288310
}
289311

290312
res <- list(
@@ -301,8 +323,9 @@
301323
#' @export
302324
.convert_xy_to_form_new <- function(object, new_data) {
303325
new_data <- new_data[, object$x_var, drop = FALSE]
304-
if (!is.data.frame(new_data))
326+
if (!is.data.frame(new_data)) {
305327
new_data <- as.data.frame(new_data)
328+
}
306329
new_data
307330
}
308331

R/descriptors.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ get_descr_df <- function(formula, data) {
116116

117117
tmp_dat <-
118118
.convert_form_to_xy_fit(formula,
119-
data,
120-
indicators = "none",
121-
remove_intercept = TRUE)
119+
data,
120+
indicators = "none",
121+
remove_intercept = TRUE)
122122

123123
if(is.factor(tmp_dat$y)) {
124124
.lvls <- function() {

0 commit comments

Comments
 (0)