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$
|
^\.claude$
|
||||||
^\.playwright-mcp$
|
^\.playwright-mcp$
|
||||||
^civilytics-site\.png$
|
^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)
|
#' Add a Civilytics logo to a ggplot (pipe-friendly)
|
||||||
#'
|
#'
|
||||||
#' A convenience wrapper that creates the logo grob and attaches it below the
|
#' 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() +
|
#' geom_point() +
|
||||||
#' theme_civilytics() |>
|
#' theme_civilytics()) |>
|
||||||
#' civilytics_logo()
|
#' 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 plot A ggplot object.
|
||||||
#' @param type Character. `"wordmark"` (default) or `"mark"`. Passed to
|
#' @param type Character. `"wordmark"` (default) or `"mark"`. Passed to
|
||||||
#' [make_logo_grob()].
|
#' [make_logo_grob()].
|
||||||
@@ -233,17 +233,17 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
|||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
#' library(ggplot2); library(grid)
|
#' library(ggplot2); library(grid)
|
||||||
#'
|
#'
|
||||||
#' # Pipe-friendly usage
|
#' # Pipe usage — parentheses required around the ggplot chain
|
||||||
#' ggplot(mpg, aes(displ, hwy)) +
|
#' (ggplot(mpg, aes(displ, hwy)) +
|
||||||
#' geom_point() +
|
#' geom_point() +
|
||||||
#' theme_civilytics() |>
|
#' theme_civilytics()) |>
|
||||||
#' civilytics_logo() |>
|
#' civilytics_logo() |>
|
||||||
#' grid.draw()
|
#' grid.draw()
|
||||||
#'
|
#'
|
||||||
#' # Dark theme with mark
|
#' # Dark theme with mark
|
||||||
#' ggplot(mpg, aes(displ, hwy)) +
|
#' (ggplot(mpg, aes(displ, hwy)) +
|
||||||
#' geom_point() +
|
#' geom_point() +
|
||||||
#' theme_civilytics_dark() |>
|
#' theme_civilytics_dark()) |>
|
||||||
#' civilytics_logo(variant = "dark", type = "mark") |>
|
#' civilytics_logo(variant = "dark", type = "mark") |>
|
||||||
#' grid.draw()
|
#' grid.draw()
|
||||||
#' }
|
#' }
|
||||||
|
|||||||
+11
-11
@@ -29,34 +29,34 @@ A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
|||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
A convenience wrapper that creates the logo grob and attaches it below the
|
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{
|
\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() +
|
geom_point() +
|
||||||
theme_civilytics() |>
|
theme_civilytics()) |>
|
||||||
civilytics_logo()
|
civilytics_logo()
|
||||||
```
|
```
|
||||||
|
|
||||||
Since `|>` has lower precedence than `+`, the entire ggplot chain is piped
|
|
||||||
as the first argument — no parentheses needed.
|
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
\dontrun{
|
\dontrun{
|
||||||
library(ggplot2); library(grid)
|
library(ggplot2); library(grid)
|
||||||
|
|
||||||
# Pipe-friendly usage
|
# Pipe usage — parentheses required around the ggplot chain
|
||||||
ggplot(mpg, aes(displ, hwy)) +
|
(ggplot(mpg, aes(displ, hwy)) +
|
||||||
geom_point() +
|
geom_point() +
|
||||||
theme_civilytics() |>
|
theme_civilytics()) |>
|
||||||
civilytics_logo() |>
|
civilytics_logo() |>
|
||||||
grid.draw()
|
grid.draw()
|
||||||
|
|
||||||
# Dark theme with mark
|
# Dark theme with mark
|
||||||
ggplot(mpg, aes(displ, hwy)) +
|
(ggplot(mpg, aes(displ, hwy)) +
|
||||||
geom_point() +
|
geom_point() +
|
||||||
theme_civilytics_dark() |>
|
theme_civilytics_dark()) |>
|
||||||
civilytics_logo(variant = "dark", type = "mark") |>
|
civilytics_logo(variant = "dark", type = "mark") |>
|
||||||
grid.draw()
|
grid.draw()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,8 +190,11 @@ test_that("civilytics_logo returns a grob", {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test_that("civilytics_logo works with pipe operator", {
|
test_that("civilytics_logo works with pipe operator", {
|
||||||
p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + theme_civilytics()
|
# |> has higher precedence than +, so parens are required
|
||||||
result <- suppressWarnings(p |> civilytics_logo())
|
result <- suppressWarnings(
|
||||||
|
(ggplot(mpg, aes(displ, hwy)) + geom_point() + theme_civilytics()) |>
|
||||||
|
civilytics_logo()
|
||||||
|
)
|
||||||
expect_s3_class(result, "grob")
|
expect_s3_class(result, "grob")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user