fix: correct pipe precedence docs — |> binds tighter than +
R-CMD-check / R CMD check (push) Successful in 2m46s
R-CMD-check / R CMD check (push) Successful in 2m46s
R's |> has higher precedence than +, so parentheses ARE required
around the ggplot chain before piping to civilytics_logo():
(ggplot(df, aes(x, y)) + geom_point() + theme_civilytics()) |>
civilytics_logo()
Updated docs, examples, and pipe test accordingly.
This commit is contained in:
@@ -9,3 +9,4 @@
|
||||
^\.claude$
|
||||
^\.playwright-mcp$
|
||||
^civilytics-site\.png$
|
||||
^Rplots\.pdf$
|
||||
|
||||
@@ -205,18 +205,18 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
||||
#' Add a Civilytics logo to a ggplot (pipe-friendly)
|
||||
#'
|
||||
#' A convenience wrapper that creates the logo grob and attaches it below the
|
||||
#' plot in one call. Designed for use with the base pipe `|>`:
|
||||
#' plot in one call. Designed for use with the base pipe `|>`.
|
||||
#'
|
||||
#' **Important:** R's `|>` has *higher* precedence than `+`, so you must
|
||||
#' wrap the ggplot chain in parentheses before piping:
|
||||
#'
|
||||
#' ```
|
||||
#' ggplot(mpg, aes(displ, hwy)) +
|
||||
#' (ggplot(mpg, aes(displ, hwy)) +
|
||||
#' geom_point() +
|
||||
#' theme_civilytics() |>
|
||||
#' theme_civilytics()) |>
|
||||
#' civilytics_logo()
|
||||
#' ```
|
||||
#'
|
||||
#' Since `|>` has lower precedence than `+`, the entire ggplot chain is piped
|
||||
#' as the first argument — no parentheses needed.
|
||||
#'
|
||||
#' @param plot A ggplot object.
|
||||
#' @param type Character. `"wordmark"` (default) or `"mark"`. Passed to
|
||||
#' [make_logo_grob()].
|
||||
@@ -233,17 +233,17 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
||||
#' \dontrun{
|
||||
#' library(ggplot2); library(grid)
|
||||
#'
|
||||
#' # Pipe-friendly usage
|
||||
#' ggplot(mpg, aes(displ, hwy)) +
|
||||
#' # Pipe usage — parentheses required around the ggplot chain
|
||||
#' (ggplot(mpg, aes(displ, hwy)) +
|
||||
#' geom_point() +
|
||||
#' theme_civilytics() |>
|
||||
#' theme_civilytics()) |>
|
||||
#' civilytics_logo() |>
|
||||
#' grid.draw()
|
||||
#'
|
||||
#' # Dark theme with mark
|
||||
#' ggplot(mpg, aes(displ, hwy)) +
|
||||
#' (ggplot(mpg, aes(displ, hwy)) +
|
||||
#' geom_point() +
|
||||
#' theme_civilytics_dark() |>
|
||||
#' theme_civilytics_dark()) |>
|
||||
#' civilytics_logo(variant = "dark", type = "mark") |>
|
||||
#' grid.draw()
|
||||
#' }
|
||||
|
||||
+11
-11
@@ -29,34 +29,34 @@ A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
||||
}
|
||||
\description{
|
||||
A convenience wrapper that creates the logo grob and attaches it below the
|
||||
plot in one call. Designed for use with the base pipe `|>`:
|
||||
plot in one call. Designed for use with the base pipe `|>`.
|
||||
}
|
||||
\details{
|
||||
**Important:** R's `|>` has *higher* precedence than `+`, so you must
|
||||
wrap the ggplot chain in parentheses before piping:
|
||||
|
||||
```
|
||||
ggplot(mpg, aes(displ, hwy)) +
|
||||
(ggplot(mpg, aes(displ, hwy)) +
|
||||
geom_point() +
|
||||
theme_civilytics() |>
|
||||
theme_civilytics()) |>
|
||||
civilytics_logo()
|
||||
```
|
||||
|
||||
Since `|>` has lower precedence than `+`, the entire ggplot chain is piped
|
||||
as the first argument — no parentheses needed.
|
||||
}
|
||||
\examples{
|
||||
\dontrun{
|
||||
library(ggplot2); library(grid)
|
||||
|
||||
# Pipe-friendly usage
|
||||
ggplot(mpg, aes(displ, hwy)) +
|
||||
# Pipe usage — parentheses required around the ggplot chain
|
||||
(ggplot(mpg, aes(displ, hwy)) +
|
||||
geom_point() +
|
||||
theme_civilytics() |>
|
||||
theme_civilytics()) |>
|
||||
civilytics_logo() |>
|
||||
grid.draw()
|
||||
|
||||
# Dark theme with mark
|
||||
ggplot(mpg, aes(displ, hwy)) +
|
||||
(ggplot(mpg, aes(displ, hwy)) +
|
||||
geom_point() +
|
||||
theme_civilytics_dark() |>
|
||||
theme_civilytics_dark()) |>
|
||||
civilytics_logo(variant = "dark", type = "mark") |>
|
||||
grid.draw()
|
||||
}
|
||||
|
||||
@@ -190,8 +190,11 @@ test_that("civilytics_logo returns a grob", {
|
||||
})
|
||||
|
||||
test_that("civilytics_logo works with pipe operator", {
|
||||
p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + theme_civilytics()
|
||||
result <- suppressWarnings(p |> civilytics_logo())
|
||||
# |> has higher precedence than +, so parens are required
|
||||
result <- suppressWarnings(
|
||||
(ggplot(mpg, aes(displ, hwy)) + geom_point() + theme_civilytics()) |>
|
||||
civilytics_logo()
|
||||
)
|
||||
expect_s3_class(result, "grob")
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user