Skip to content

Allow column vector inputs #2635

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

Merged
merged 1 commit into from
May 19, 2018
Merged
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
13 changes: 6 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
(#2610).

* Error: Column `y` must be a 1d atomic vector or a list
Internally, ggplot2 now uses `as.data.frame(tibble::as_tibble(x))` to
convert a list into a data frame. This improves ggplot2's support for

Internally, ggplot2 now uses `as.data.frame(tibble::as_tibble(x))` to
convert a list into a data frame. This improves ggplot2's support for
list-columns (needed for sf support), at a small cost: you can no longer
use matrix-columns. These are rarely used but are produced by `scale()`;
to continue to use `scale()` you'll need to wrap it with `as.numeric()`,
e.g. `as.numeric(scale(x))`.

use matrix-columns. Note that unlike tibble we still allow column vectors
such as returned by `base::scale()` because of their widespread use.

* Error: More than one expression parsed

Previously `aes_string(x = c("a", "b", "c"))` silently returned
Expand Down
2 changes: 1 addition & 1 deletion R/geom-.r
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Geom <- ggproto("Geom",
missing_eval <- compact(missing_eval)

if (empty(data)) {
data <- as.data.frame(tibble::as_tibble(missing_eval))
data <- as_gg_data_frame(missing_eval)
} else {
data[names(missing_eval)] <- missing_eval
}
Expand Down
2 changes: 1 addition & 1 deletion R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ Layer <- ggproto("Layer", NULL,
evaled$PANEL <- data$PANEL
}
evaled <- lapply(evaled, unname)
evaled <- as.data.frame(tibble::as_tibble(evaled))
evaled <- as_gg_data_frame(evaled)
evaled <- add_group(evaled)
evaled
},
Expand Down
16 changes: 16 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,19 @@ seq_asc <- function(to, from) {
# Needed to trigger package loading
#' @importFrom tibble tibble
NULL

# Check inputs with tibble but allow column vectors (see #2609 and #2374)
as_gg_data_frame <- function(x) {
x <- lapply(x, validate_column_vec)
as.data.frame(tibble::as_tibble(x))
}
validate_column_vec <- function(x) {
if (is_column_vec(x)) {
dim(x) <- NULL
}
x
}
is_column_vec <- function(x) {
dims <- dim(x)
length(dims) == 2L && dims[[2]] == 1L
}
7 changes: 7 additions & 0 deletions tests/testthat/test-layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ test_that("unknown NULL asthetic doesn't create warning (#1909)", {
expect_warning(geom_point(aes(blah = NULL)), NA)
})

test_that("column vectors are allowed (#2609)", {
df <- data.frame(x = 1:10)
df$y <- scale(1:10) # Returns a column vector
p <- ggplot(df, aes(x, y))
expect_is(layer_data(p), "data.frame")
})

# Data extraction ---------------------------------------------------------

test_that("layer_data returns a data.frame", {
Expand Down