Skip to content

Commit

Permalink
Merge pull request #38 from tidymodels/use-glue
Browse files Browse the repository at this point in the history
Use glue
  • Loading branch information
EmilHvitfeldt authored Jul 26, 2024
2 parents 2239f9b + 5991e23 commit 172ad85
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 64 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Suggests:
dbplyr,
dtplyr,
embed,
glue,
hardhat,
jsonlite,
kknn,
Expand Down
2 changes: 1 addition & 1 deletion R/orbital.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ print.orbital_class <- function(x, ..., digits = 7, truncate = TRUE) {
x <- unclass(x)
x <- pretty_print(x, digits)

eqs <- paste0(names(x), " = ", x)
eqs <- glue::glue("{names(x)} = {x}")

if (truncate) {
eqs_lens <- nchar(eqs)
Expand Down
9 changes: 5 additions & 4 deletions R/recipes-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ lencode_helper <- function(x, all_vars) {
values <- mapping[["..value"]][!new_ind]
default <- mapping[["..value"]][new_ind]

eq <- paste0(col, " == \"", levels, "\" ~ ", values)
eq <- c(eq, paste0(".default = ", default))
eq <- glue::glue("{col} == \"{levels}\" ~ {values}")
eq <- c(eq, glue::glue(".default = {default}"))
eq <- paste(eq, collapse = ", ")
eq <- glue::glue("dplyr::case_when({eq})")

eq <- paste0("dplyr::case_when(", paste0(eq, collapse = ", "), ")")
names(eq) <- col
out <- c(out, eq)
}
Expand All @@ -39,7 +40,7 @@ pca_helper <- function(rot, prefix, all_vars) {

out <- character(length(all_vars))
for (i in seq_along(all_vars)) {
out[i] <- paste(row_nms, "*", rot[, i], collapse = " + ")
out[i] <- paste(glue::glue("{row_nms} * {rot[, i]}"), collapse = " + ")
}

names(out) <- all_vars
Expand Down
1 change: 1 addition & 0 deletions R/recipes.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#' @export
orbital.recipe <- function(x, eqs = NULL, ...) {
rlang::check_installed("glue")
if (!recipes::fully_trained(x)) {
cli::cli_abort("recipe must be fully trained.")
}
Expand Down
4 changes: 2 additions & 2 deletions R/step_bin2factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ orbital.step_bin2factor <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0(
"ifelse(", columns, " == 1, \"", x$levels[1], "\", \"", x$levels[2], "\")"
out <- glue::glue(
"ifelse({columns} == 1, \"{x$levels[1]}\", \"{x$levels[2]}\")"
)

names(out) <- columns
Expand Down
4 changes: 2 additions & 2 deletions R/step_boxcox.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ orbital.step_BoxCox <- function(x, all_vars, ...) {

out <- ifelse(
abs(lambdas) < 0.001,
paste0("log(", names(lambdas), ")"),
paste0("(", names(lambdas), " ^ ", lambdas, " - 1) / ", lambdas)
glue::glue("log({names(lambdas)})"),
glue::glue("({names(lambdas)} ^ {lambdas} - 1) / {lambdas}")
)
out
}
2 changes: 1 addition & 1 deletion R/step_center.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ orbital.step_center <- function(x, all_vars, ...) {
used_vars <- names(means) %in% all_vars
means <- means[used_vars]

out <- paste0(names(means), " - ", means)
out <- glue::glue("{names(means)} - {means}")
names(out) <- names(means)
out
}
17 changes: 9 additions & 8 deletions R/step_discretize.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@ orbital.step_discretize <- function(x, all_vars, ...) {
eq <- character()

if (object$keep_na) {
eq <- c(eq, paste0("is.na(", col, ")"))
eq <- c(eq, glue::glue("is.na({col})"))
}

eq <- c(eq, paste0(col, " < ", object$breaks[2]))
eq <- c(eq, glue::glue("{col} < {object$breaks[2]}"))
if (object$bins > 2) {
low <- seq(2, object$bins - 1)
high <- low + 1

eq <- c(eq,
paste0(object$breaks[low], " < ", col, " & ",
col, " <= ", object$breaks[high])
glue::glue("{object$breaks[low]} < {col} & {col} <= {object$breaks[high]}")
)
}

eq <- c(eq, paste0(utils::tail(object$breaks, 2)[1], " <= ", col))
if (object$bins != 1) {
eq <- c(eq, glue::glue("{utils::tail(object$breaks, 2)[1]} <= {col}"))
}

eq <- paste0(eq, " ~ \"", paste0(object$prefix, object$labels), "\"")
eq <- glue::glue("{eq} ~ \"{object$prefix}{object$labels}\"")
eq <- paste(eq, collapse = ", ")
eq <- glue::glue("dplyr::case_when({eq})")

eq <- paste0("dplyr::case_when(", paste0(eq, collapse = ", "), ")")
names(eq) <- col
out <- c(out, eq)
}
Expand Down
2 changes: 1 addition & 1 deletion R/step_dummy.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ orbital.step_dummy <- function(x, all_vars, ...) {
levels <- levels[level_ind]
var_out <- var_out[level_ind]

eqs <- paste0("as.numeric(", var, " == \"", levels, "\")")
eqs <- glue::glue("as.numeric({var} == \"{levels}\")")
out <- c(out, stats::setNames(eqs, var_out))
}

Expand Down
5 changes: 2 additions & 3 deletions R/step_impute_mean.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ orbital.step_impute_mean <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0(
"ifelse(is.na(", names(means), "), ", means ,", ", names(means), ")"
)
out <- glue::glue("ifelse(is.na({names(means)}), {means}, {names(means)})")

names(out) <- names(means)
out
}
5 changes: 3 additions & 2 deletions R/step_impute_median.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ orbital.step_impute_median <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0(
"ifelse(is.na(", names(medians), "), ", medians ,", ", names(medians), ")"
out <- glue::glue(
"ifelse(is.na({names(medians)}), {medians}, {names(medians)})"
)

names(out) <- names(medians)
out
}
5 changes: 2 additions & 3 deletions R/step_impute_mode.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ orbital.step_impute_mode <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0(
"ifelse(is.na(", names(modes), "), \"", modes ,"\", ", names(modes), ")"
)
out <- glue::glue("ifelse(is.na({names(modes)}), \"{modes}\", {names(modes)})")

names(out) <- names(modes)
out
}
5 changes: 3 additions & 2 deletions R/step_indicate_na.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @export
orbital.step_indicate_na <- function(x, all_vars, ...) {
cols <- x$columns
col_names <- paste0(x$prefix, "_", cols)
col_names <- glue::glue("{x$prefix}_{cols}")

used_vars <- col_names %in% all_vars
cols <- cols[used_vars]
Expand All @@ -11,7 +11,8 @@ orbital.step_indicate_na <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0("as.integer(is.na(", cols, "))")
out <- glue::glue("as.integer(is.na({cols}))")

names(out) <- col_names
out
}
5 changes: 3 additions & 2 deletions R/step_inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ orbital.step_inverse <- function(x, all_vars, ...) {
}

if (offset == 0) {
out <- paste0("1 / ", columns)
out <- glue::glue("1 / {columns}")
} else {
out <- paste0("1 / (", columns, " + ", offset, ")")
out <- glue::glue("1 / ({columns} + {offset})")
}

names(out) <- names(columns)
out
}
8 changes: 4 additions & 4 deletions R/step_lag.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ orbital.step_lag <- function(x, all_vars, ...) {
}

configs <- expand.grid(lag = x$lag, columns = x$columns)
col_names <- paste0(x$prefix, configs$lag, "_", configs$columns)
col_names <- glue::glue("{x$prefix}{configs$lag}_{configs$columns}")

used_vars <- col_names %in% all_vars
configs <- configs[used_vars, ]
col_names <- col_names[used_vars]

out <- paste0(
"dplyr::lag(", configs$columns, ", ", configs$lag, ", default = ",
x$default, ")"
out <- glue::glue(
"dplyr::lag({configs$columns}, {configs$lag}, default = {x$default})"
)

names(out) <- col_names
out
}
9 changes: 4 additions & 5 deletions R/step_log.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ orbital.step_log <- function(x, all_vars, ...) {
columns <- columns[used_vars]

if (x$signed) {
out <- paste0(
"ifelse(abs(", columns, ") < 1, 0, sign(", columns, ") * log(abs(",
columns, "), base = ", x$base, "))"
out <- glue::glue(
"ifelse(abs({columns}) < 1, 0, sign({columns}) * log(abs({columns}), base = {x$base}))"
)
} else {
out <- paste0(
"log(", columns, " + ", x$offset, ", base = ", x$base, ")"
out <- glue::glue(
"log({columns} + {x$offset}, base = {x$base})"
)
}

Expand Down
3 changes: 2 additions & 1 deletion R/step_normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ orbital.step_normalize <- function(x, all_vars, ...) {
means <- means[used_vars]
sds <- sds[used_vars]

out <- paste0("(", names(means), " - ", means ,") / ", sds)
out <- glue::glue("({names(means)} - {means}) / {sds}")

names(out) <- names(means)
out
}
11 changes: 5 additions & 6 deletions R/step_novel.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ orbital.step_novel <- function(x, all_vars, ...) {
out <- character()
for (col in names(objects)) {
levels <- objects[[col]]
levels <- paste0("\"", levels, "\"")
levels <- paste0(levels, collapse = ", ")
levels <- paste0("c(", levels, ")")
out[[col]] <- paste0(
"ifelse(is.na(", col, "), NA, ifelse(", col, " %in% ", levels, ", ", col,
", \"", x$new_level,"\"))"
levels <- glue::glue("\"{levels}\"")
levels <- paste(levels, collapse = ", ")
levels <- glue::glue("c({levels})")
out[[col]] <- glue::glue(
"ifelse(is.na({col}), NA, ifelse({col} %in% {levels}, {col}, \"{x$new_level}\"))"
)
}
out
Expand Down
11 changes: 5 additions & 6 deletions R/step_other.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ orbital.step_other <- function(x, all_vars, ...) {
next
}
levels <- objects[[col]]$keep
levels <- paste0("\"", levels, "\"")
levels <- paste0(levels, collapse = ", ")
levels <- paste0("c(", levels, ")")
out[[col]] <- paste0(
"ifelse(is.na(", col, "), NA, ifelse(", col, " %in% ", levels, ", ", col,
", \"", objects[[col]]$other, "\"))"
levels <- glue::glue("\"{levels}\"")
levels <- paste(levels, collapse = ", ")
levels <- glue::glue("c({levels})")
out[[col]] <- glue::glue(
"ifelse(is.na({col}), NA, ifelse({col} %in% {levels}, {col}, \"{objects[[col]]$other}\"))"
)
}
out
Expand Down
12 changes: 7 additions & 5 deletions R/step_range.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ orbital.step_range <- function(x, all_vars, ...) {
min <- x$min
max <- x$max

out <- paste0(
"(", cols, " - ", ranges["mins", ], ") * (", max, " - ", min, ")/(",
ranges["maxs", ], " - ", ranges["mins", ], ") + ", min
range_mins <- ranges["mins", ]
range_maxs <- ranges["maxs", ]

out <- glue::glue(
"({cols} - {range_mins}) * ({max} - {min})/({range_maxs} - {range_mins}) + {min}"
)

if (is.null(x$clipping) || isTRUE(x$clipping)) {
out <- paste0("pmax(", out, ", ", min, ")")
out <- paste0("pmin(", out, ", ", max, ")")
out <- glue::glue("pmax({out}, {min})")
out <- glue::glue("pmin({out}, {max})")
}

names(out) <- cols
Expand Down
2 changes: 1 addition & 1 deletion R/step_ratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ orbital.step_ratio <- function(x, all_vars, ...) {
columns <- columns[used_vars, ]
col_names <- col_names[used_vars]

out <- paste0(columns$top, " / ", columns$bottom)
out <- glue::glue("{columns$top} / {columns$bottom}")
names(out) <- col_names
out
}
2 changes: 1 addition & 1 deletion R/step_scale.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ orbital.step_scale <- function(x, all_vars, ...) {
used_vars <- names(sds) %in% all_vars
sds <- sds[used_vars]

out <- paste0(names(sds) ," / ", sds)
out <- glue::glue("{names(sds)} / {sds}")
names(out) <- names(sds)
out
}
3 changes: 2 additions & 1 deletion R/step_sqrt.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ orbital.step_sqrt <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0("sqrt(", columns, ")")
out <- glue::glue("sqrt({columns})")

names(out) <- names(columns)
out
}
5 changes: 2 additions & 3 deletions R/step_unknown.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ orbital.step_unknown <- function(x, all_vars, ...) {
return(NULL)
}

out <- paste0(
"ifelse(is.na(", vars, "), \"", x$new_level ,"\", ", vars, ")"
)
out <- glue::glue("ifelse(is.na({vars}), \"{x$new_level}\", {vars})")

names(out) <- vars
out
}

0 comments on commit 172ad85

Please sign in to comment.