Skip to content
Closed
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
18 changes: 18 additions & 0 deletions R/pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@
#' 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 `|>`.
#' To read about the differences, read this `tidyverse`
#' [blog post](https://www.tidyverse.org/blog/2023/04/base-vs-magrittr-pipe/)
#' describing some special cases where using the base R pipe requires handling
#' different from the `magrittr` pipe, including using
#' * `x |> f(1, y = _)` instead of `x %>% f(1, y = .)`,
#' * `x |> (\(x) x[[1]]))()` instead of `x %>% .[[1]]`,
#' * `x |> (\(x) f(a = x, b = x))()` instead of `x %>% f(a = ., b = .)`, and
#' * `x |> f()` instead of `x %>% f`.
#'
#' @export
#'
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' use_pipe()
#' }
use_pipe <- function(export = TRUE) {
lifecycle::deprecate_warn(when = "3.2.2", what = "use_pipe()")

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

Expand Down
14 changes: 14 additions & 0 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
#' @param lhs A value or the magrittr placeholder.
#' @param rhs A function call using the magrittr semantics.
#' @return The result of calling `rhs(lhs)`.
NULL
20 changes: 20 additions & 0 deletions man/pipe.Rd

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

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.

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

# use_pipe() should produce a lifecycle deprecated warning

Code
create_local_package()
use_package_doc()
use_pipe(export = FALSE)
Condition
Warning:
`use_pipe()` was deprecated in usethis 3.2.2.

13 changes: 13 additions & 0 deletions tests/testthat/test-pipe.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
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", {
expect_snapshot({
create_local_package()
use_package_doc()
use_pipe(export = FALSE)
})
})


Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

Loading