Skip to content
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# usethis (development version)

* `use_pipe()` is now deprecated (@math-mcshane, #2124).

* `use_claude_code()` is an experimental helper to set up Claude code in an R package in the same way as the tidyverse team (#2195).

* Removes deprecated `use_tidy_style()` from to-do's from upkeep (@edgararuiz)

* `pr_resume()` (without a specific `branch`) and `pr_fetch()` (without a specific `number`) no longer error when a branch name contains curly braces (#2107, @jonthegeek).
Expand Down
29 changes: 20 additions & 9 deletions R/pipe.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
#' Use magrittr's pipe in your package
#'
#' Does setup necessary to use magrittr's pipe operator, `%>%` in your package.
#' This function requires the use of \pkg{roxygen2}.
#' * Adds magrittr to "Imports" in `DESCRIPTION`.
#' * Imports the pipe operator specifically, which is necessary for internal
#' use.
#' * Exports the pipe operator, if `export = TRUE`, which is necessary to make
#' `%>%` available to the users of your package.
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' The base R pipe has been available since R 4.1.0 and we recommend using it
#' in all actively maintained and new work, both inside and outside packages.
#' The `magrittr` pipe (`%>%`) can usually just be replaced with base R's `|>`,
#' with a few things to keep in mind:
#'
#' * Instead of `x %>% f`, use `x |> f()`.
#' * Instead of `x %>% f(1, y = .)`, use `x |> f(1, y = _)`.
#' * Instead of `x %>% f(a = ., b = .)`, define a new helper function.
#'
#' Learn more in <https://www.tidyverse.org/blog/2023/04/base-vs-magrittr-pipe/>.
#'
#' @param export If `TRUE`, the file `R/utils-pipe.R` is added, which provides
#' the roxygen template to import and re-export `%>%`. If `FALSE`, the necessary
#' roxygen directive is added, if possible, or otherwise instructions are given.
#'
#' @export
#'
#' @keywords internal
#' @examples
#' \dontrun{
#' use_pipe()
#' }
use_pipe <- function(export = TRUE) {
lifecycle::deprecate_warn(
when = "3.2.2",
what = "usethis::use_pipe()",
details = "We recommend using the base R pipe instead."
)

check_is_package("use_pipe()")
check_uses_roxygen("use_pipe()")

Expand Down
19 changes: 12 additions & 7 deletions man/use_pipe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/pipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
Output
[1] "#' @importFrom magrittr %>%"

# use_pipe() should produce a lifecycle deprecated warning

Code
use_pipe(export = FALSE)
Condition
Warning:
`use_pipe()` was deprecated in usethis 3.2.2.
i We recommend using the base R pipe instead.

9 changes: 9 additions & 0 deletions tests/testthat/test-pipe.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
test_that("use_pipe() requires a package", {
withr::local_options(lifecycle_verbosity = "quiet")
create_local_project()
expect_usethis_error(use_pipe(), "not an R package")
})

test_that("use_pipe(export = TRUE) adds promised file, Imports magrittr", {
withr::local_options(lifecycle_verbosity = "quiet")
create_local_package()
use_pipe(export = TRUE)
expect_equal(desc::desc_get_field("Imports"), "magrittr")
expect_proj_file("R", "utils-pipe.R")
})

test_that("use_pipe(export = FALSE) adds roxygen to package doc", {
withr::local_options(lifecycle_verbosity = "quiet")
create_local_package()
use_package_doc()
use_pipe(export = FALSE)
expect_equal(desc::desc_get_field("Imports"), "magrittr")

expect_snapshot(roxygen_ns_show())
})

test_that("use_pipe() should produce a lifecycle deprecated warning", {
create_local_package()
use_package_doc()
expect_snapshot(use_pipe(export = FALSE))
})