Skip to content
Open
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
23 changes: 23 additions & 0 deletions R/pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,36 @@
#' the roxygen template to import and re-export `%>%`. If `FALSE`, the necessary
#' roxygen directive is added, if possible, or otherwise instructions are given.
#'
#'#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' The base R pipe has been available since R 4.1.0, which handles most cases
#' the `magrittr` pipe handles -- `%>%` can usually just be replaced with `|>`.
#' However, other differences include:
#' * instead of `x %>% f(1, y = .)`, use `x |> f(1, y = _)`,
#' * instead of `x %>% .[[1]]`, use `x |> (\(x) x[[1]]))()`,
#' * instead of `x %>% f(a = ., b = .)`, use `x |> (\(x) f(a = x, b = x))()`,
#' and
#' * instead of `x %>% f`, use `x |> f()`.
#' To read about these differences, read this `tidyverse`
#' [blog post](https://www.tidyverse.org/blog/2023/04/base-vs-magrittr-pipe/).
#'
#' @export
#'
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' use_pipe()
#' }
use_pipe <- function(export = TRUE) {
lifecycle::deprecate_warn(
when = "3.2.2",
what = "usethis::use_pipe()",
details = "It is recommended to use the base R pipe |> in your package
instead; it does not require an import."
)

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

Expand Down
19 changes: 18 additions & 1 deletion 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 It is recommended to use the base R pipe |> in your package instead; it does not require an import.

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))
})
Loading