Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move {tibble} to suggests #5990

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Imports:
rlang (>= 1.1.0),
scales (>= 1.3.0),
stats,
tibble,
vctrs (>= 0.6.0),
withr (>= 2.5.0)
Suggests:
Expand All @@ -68,6 +67,7 @@ Suggests:
sf (>= 0.7-3),
svglite (>= 2.1.2),
testthat (>= 3.1.5),
tibble,
vdiffr (>= 1.0.6),
xml2
Enhances:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,6 @@ importFrom(grid,unit)
importFrom(lifecycle,deprecated)
importFrom(scales,alpha)
importFrom(stats,setNames)
importFrom(tibble,tibble)
importFrom(utils,.DollarNames)
importFrom(utils,head)
importFrom(utils,tail)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* {tibble} is now suggested instead of imported (@teunbrand, #5986)
* All position scales now use the same definition of `x` and `y` aesthetics.
This lets uncommon aesthetics like `xintercept` expand scales as usual.
(#3342, #4966, @teunbrand)
Expand Down
2 changes: 1 addition & 1 deletion R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ is_facets <- function(x) {
# but that seems like a reasonable tradeoff.
eval_facets <- function(facets, data, possible_columns = NULL) {
vars <- compact(lapply(facets, eval_facet, data, possible_columns = possible_columns))
data_frame0(tibble::as_tibble(vars))
data_frame0(!!!vars)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We potentially lose some tibble validation checks here, but if we would want to validate here, I think it'd be better to control the validation ourselves.

Copy link
Member

Choose a reason for hiding this comment

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

I (and probably we all) agree with you in that the validation ggplot2 wants isn't necessarily the same as the one provided by tibble (cf. #3018 (comment))! But, I feel a bit anxious that no validation will be applied here after we move tibble to suggests.

Defining a concrete spec of the validation is a tough job and takes time. So, it might be realistic to keep relying on tibble at the moment. I think we can assume an ordinary user uses tibble anyway via tidyverse.

Copy link
Collaborator Author

@teunbrand teunbrand Jul 10, 2024

Choose a reason for hiding this comment

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

I feel a bit anxious that no validation will be applied here

Yeah I empathise with this, but let me try to convince you this change wouldn't make the situation any worse than it currently is.

tibble()'s validation is essentially vec_is(column) || is.expression(column) in tibble:::is_valid_col() combined with tibble:::make_valid_col(), which is just turning the expression into a list so that it complies with {vctrs}.

The validation in ggplot2:::data_frame0() also has the vec_is() constraint (via vctrs C internals I presume), but not the expression coersion. Seeing as we don't really accept expression columns as facet specificiation, this isn't a loss.

In addition to the similarity, the current validation isn't constraining the data types to the ones we can use as facet variables. Current validation happily accepts matrix or list columns which we then struggle to use downstream:

library(ggplot2)
df <- mpg
df$mtx <- matrix(1:4, nrow = 1, ncol = 4)
df$lst <- list(4)

p <- ggplot(df, aes(displ, hwy)) +
  geom_point()
p + facet_wrap(~mtx)
#> Warning in `[<-.data.frame`(`*tmp*`, , value = list(mtx = structure(c(1L, :
#> replacement element 1 has 936 rows to replace 234 rows
#> Warning in `[<-.data.frame`(`*tmp*`, , value = list(PANEL = structure(1:4,
#> levels = c("1", : replacement element 4 has 16 rows to replace 4 rows
#> Warning in mat[index] <- unlist(unname(strips), recursive = FALSE)[[position]]:
#> number of items to replace is not a multiple of replacement length

p + facet_wrap(~lst)
#> Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...): 'x' must be atomic

Created on 2024-07-10 with reprex v2.1.0

Copy link
Member

@yutannihilation yutannihilation Jul 10, 2024

Choose a reason for hiding this comment

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

Ah, thanks for the details. Then, I think I'm now convinced! The issue I referred to was before the vctrs integration, so it seems I was outdated...

I now feel this pull request is a go, but I'd like to wait for another approval to make sure.

}
eval_facet <- function(facet, data, possible_columns = NULL) {
# Treat the case when `facet` is a quosure of a symbol specifically
Expand Down
12 changes: 5 additions & 7 deletions R/position-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@
#'
#' # Negative values -----------------------------------------------------------
#'
#' df <- tibble::tribble(
#' ~x, ~y, ~grp,
#' "a", 1, "x",
#' "a", 2, "y",
#' "b", 1, "x",
#' "b", 3, "y",
#' "b", -1, "y"
#' df <- data.frame(
#' x = rep(c("a", "b"), 2:3),
#' y = c(1, 2, 1, 3, -1),
#' grp = c("x", "y", "x", "y", "y")
#' )
#'
#' ggplot(data = df, aes(x, y, group = grp)) +
#' geom_col(aes(fill = grp), position = position_stack(reverse = TRUE)) +
#' geom_hline(yintercept = 0)
Expand Down
4 changes: 2 additions & 2 deletions R/summarise-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ summarise_layout = function(p) {
l <- p$layout

layout <- l$layout
layout <- tibble(
layout <- data_frame0(
panel = l$layout$PANEL,
row = l$layout$ROW,
col = l$layout$COL
Expand Down Expand Up @@ -134,7 +134,7 @@ summarise_layers <- function(p) {

# This currently only returns the mappings, but in the future, other
# information could be added.
tibble(
data_frame0(
mapping = layer_mappings
)
}
4 changes: 0 additions & 4 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,6 @@ seq_asc <- function(to, from) {
}
}

# Needed to trigger package loading
#' @importFrom tibble tibble
NULL

# Wrapping vctrs data_frame constructor with no name repair
data_frame0 <- function(...) data_frame(..., .name_repair = "minimal")

Expand Down
12 changes: 5 additions & 7 deletions man/position_stack.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-geom-quantile.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("geom_quantile matches quantile regression", {

set.seed(6531)
x <- rnorm(10)
df <- tibble::tibble(
df <- data_frame0(
x = x,
y = x^2 + 0.5 * rnorm(10)
)
Expand Down
9 changes: 4 additions & 5 deletions tests/testthat/test-geom-tile.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ test_that("accepts width and height aesthetics", {
geom_tile(fill = NA, colour = "black", linewidth = 1)
out <- get_layer_data(p)

boundary <- as.data.frame(tibble::tribble(
~xmin, ~xmax, ~ymin, ~ymax,
-1, 1, -1, 1,
-2, 2, -2, 2
))
boundary <- data_frame0(
xmin = c(-1, -2), xmax = c(1, 2),
ymin = c(-1, -2), ymax = c(1, 2)
)
expect_equal(out[c("xmin", "xmax", "ymin", "ymax")], boundary)
})

Expand Down
7 changes: 3 additions & 4 deletions tests/testthat/test-scale-discrete.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Missing values ----------------------------------------------------------

df <- tibble::tibble(
df <- data_frame0(
x1 = c("a", "b", NA),
x2 = factor(x1),
x3 = addNA(x2),

x2 = factor(c("a", "b", NA)),
x3 = factor(c("a", "b", NA), levels = c("a", "b", NA), exclude = NULL),
y = 1:3
)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-sec-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ test_that("sec axis works with tidy eval", {
g
}

t <- tibble(x = letters, y = seq(10, 260, 10), z = 1:26)
t <- data_frame0(x = letters, y = seq(10, 260, 10), z = 1:26)

p <- f(t, x, y, z)

Expand Down
39 changes: 12 additions & 27 deletions tests/testthat/test-stat-align.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
test_that("standard alignment works", {
df <- tibble::tribble(
~g, ~x, ~y,
"a", 1, 2,
"a", 3, 5,
"a", 5, 1,
"b", 2, 3,
"b", 4, 6,
"b", 6, 7
df <- data_frame0(
g = rep(c("a", "b"), each = 3L),
x = c(1, 3, 5, 2, 4, 6),
y = c(2, 5, 1, 3, 6, 7)
)
p <- ggplot(df, aes(x, y, fill = g)) + geom_area(color = "black")
expect_doppelganger("align two areas", p)
})

test_that("alignment with cliffs works", {
df <- tibble::tribble(
~g, ~x, ~y,
"a", 1, 2,
"a", 3, 5,
"a", 5, 1,
"b", 2, 3,
"b", 4, 3,
"b", 4, 6,
"b", 6, 7
df <- data_frame0(
g = rep(c("a", "b"), 3:4),
x = c(1, 3, 5, 2, 4, 4, 6),
y = c(2, 5, 1, 3, 3, 6, 7)
)

p <- ggplot(df, aes(x, y, fill = g)) + geom_area(color = "black")
expect_doppelganger("align two areas with cliff", p)
})

test_that("alignment with negative and positive values works", {
df <- tibble::tribble(
~g, ~x, ~y,
"a", 1, 1,
"a", 2, 4,
"a", 3, -4,
"a", 8, 0,
"b", 2, 4,
"b", 6, -4
df <- data_frame0(
g = rep(c("a", "b"), c(4L, 2L)),
x = c(1, 2, 3, 8, 2, 6),
y = c(1, 4, -4, 0, 4, -4)
)

p <- ggplot(df, aes(x, y, fill = g)) + geom_area(color = "black")
expect_doppelganger("align two areas with pos/neg y", p)
})
Expand Down
Loading