Skip to content

Replacing the magrittr pipe #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Version: 0.2.0.9000
Authors@R: c(
person("Max", "Kuhn", , "max@posit.co", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2402-136X")),
person(given = "Posit Software, PBC", role = c("cph", "fnd"))
person("Posit Software, PBC", role = c("cph", "fnd"))
)
Description: Code snippets to fit models using the tidymodels framework
can be easily created for a given data set.
Expand All @@ -13,7 +13,7 @@ URL: https://usemodels.tidymodels.org/,
https://github.com/tidymodels/usemodels
BugReports: https://github.com/tidymodels/usemodels/issues
Depends:
R (>= 3.4)
R (>= 4.1)
Imports:
cli,
clipr,
Expand All @@ -28,8 +28,7 @@ Suggests:
modeldata,
spelling,
testthat
Config/Needs/website:
tidyverse/tidytemplate
Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ import(tune)
importFrom(dplyr,one_of)
importFrom(dplyr,pull)
importFrom(dplyr,select)
importFrom(recipes,"%>%")
importFrom(recipes,all_predictors)
importFrom(recipes,recipe)
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# usemodels (development version)

* Transition from the magrittr pipe to the base R pipe.

* Added `use_nnet()`, `use_rpart()`, `use_bag_tree_rpart()`, `use_mgcv()`, `use_dbarts()`, `use_mixOmics()`, `use_xrf()`.

* Fix `recipe()` call when `clipboard = TRUE`


# usemodels 0.2.0

* SVM (#14) and C5.0 (#9) models were added.
Expand Down
34 changes: 17 additions & 17 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
}
var_roles <- summary(rec)
y_cols <- var_roles$variable[var_roles$role == "outcome"]
y_dat <- rec$template %>%
dplyr::select(one_of(y_cols)) %>%
y_dat <- rec$template |>
dplyr::select(one_of(y_cols)) |>
dplyr::pull(1)
length(levels(y_dat))
}
Expand Down Expand Up @@ -57,14 +57,14 @@
res
}
pipe_value <- function(base, value) {
# Find last non-comment line, add a `%>%` to the end, then add another line
# Find last non-comment line, add a `|>` to the end, then add another line
value <- rlang::enexpr(value)
value <- rlang::expr_text(value, width = expr_width)
clean_base <- gsub("\\n", "", base)
clean_base <- trimws(base, which = "left")
not_comment <- seq_along(base)[!grepl("## ", clean_base)]
n <- max(1, max(not_comment))
base[n] <- paste(base[n], "%>%")
base[n] <- paste(base[n], "|>")
c(base, paste0("\n ", value))
}
add_comment <- function(base, value, add = TRUE, colors = TRUE) {
Expand All @@ -84,38 +84,38 @@
res
}
add_steps_dummy_vars <- function(base, hot = FALSE, add = FALSE, colors = TRUE) {
base <- base %>%
base <- base |>
pipe_value(step_novel(all_nominal_predictors()))
if (hot) {
base <- base %>%
add_comment(dummy_hot_msg, add, colors = colors) %>%
base <- base |>
add_comment(dummy_hot_msg, add, colors = colors) |>
pipe_value(step_dummy(all_nominal_predictors(), one_hot = TRUE))
} else {
base <- base %>%
add_comment(dummy_msg, add, colors = colors) %>%
base <- base |>
add_comment(dummy_msg, add, colors = colors) |>
pipe_value(step_dummy(all_nominal_predictors()))
}
base
}
add_steps_normalization <- function(base) {
base %>%
pipe_value(step_zv(all_predictors())) %>%
base |>
pipe_value(step_zv(all_predictors())) |>
pipe_value(step_normalize(all_numeric_predictors()))
}
factor_check <- function(base, rec, add, colors = TRUE) {
var_roles <- summary(rec)
nominal <- var_roles$variable[var_roles$type == "nominal"]
is_str <-
purrr::map_lgl(
rec$template %>% dplyr::select(dplyr::one_of(nominal)),
rec$template |> dplyr::select(dplyr::one_of(nominal)),
rlang::is_character
)
if (any(is_str)) {
selector <- rlang::expr(one_of(!!!nominal[is_str]))
step_expr <- rlang::expr(step_string2factor(!!selector))
base <-
base %>%
add_comment(string_to_factor_msg, add = add, colors = colors) %>%
base |>
add_comment(string_to_factor_msg, add = add, colors = colors) |>

Check warning on line 118 in R/misc.R

View check run for this annotation

Codecov / codecov/patch

R/misc.R#L117-L118

Added lines #L117 - L118 were not covered by tests
pipe_value(!!step_expr)
}
base
Expand All @@ -135,9 +135,9 @@
}

template_workflow <- function(prefix) {
paste0(prefix, "_workflow") %>%
assign_value(workflow()) %>%
pipe_value(add_recipe(!!rlang::sym(paste0(prefix, "_recipe")))) %>%
paste0(prefix, "_workflow") |>
assign_value(workflow()) |>
pipe_value(add_recipe(!!rlang::sym(paste0(prefix, "_recipe")))) |>
pipe_value(add_model(!!rlang::sym(paste0(prefix, "_spec"))))
}

Expand Down
Loading