Skip to content

replace parse() with parse_safe() in geom_text() #2867

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 19 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from 14 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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
* `position_nudge()` is now more robust and nudges only in the direction
requested. This enables, for example, the horizontal nudging of boxplots
(@clauswilke, #2733).

* `geom_text(..., parse = TRUE)` now correctly renders the expected number of
items instead of silently dropping items that are not valid expressions.
Copy link
Member

Choose a reason for hiding this comment

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

Not valid isn't quite the right turn of phrase - they are valid; they're just empty. We also fixed handling of multiple expressions in a single string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, will try to make the comment more accurate.

This is also fixed for `geom_label()` and the axis labels for `geom_sf()`
(@slowkow, #2867).

# ggplot2 3.0.0

Expand Down
2 changes: 1 addition & 1 deletion R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ GeomLabel <- ggproto("GeomLabel", Geom,
label.size = 0.25) {
lab <- data$label
if (parse) {
lab <- parse(text = as.character(lab))
lab <- parse_safe(as.character(lab))
}

data <- coord$transform(data, panel_params)
Expand Down
3 changes: 1 addition & 2 deletions R/geom-text.r
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ geom_text <- function(mapping = NULL, data = NULL,
)
}


#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
Expand All @@ -176,7 +175,7 @@ GeomText <- ggproto("GeomText", Geom,
na.rm = FALSE, check_overlap = FALSE) {
lab <- data$label
if (parse) {
lab <- parse(text = as.character(lab))
lab <- parse_safe(as.character(lab))
}

data <- coord$transform(data, panel_params)
Expand Down
17 changes: 14 additions & 3 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,20 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
if (!is.null(graticule$plot12))
graticule$degree_label[!graticule$plot12] <- NA

# parse labels into expressions if required
if (any(grepl("degree", graticule$degree_label)))
graticule$degree_label <- lapply(graticule$degree_label, function(x) parse(text = x)[[1]])
# Convert the string 'degree' to the degree symbol
parse_ids <- grepl("\\bdegree\\b", graticule$degree_label)
Copy link
Member

Choose a reason for hiding this comment

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

I think this would be more informative if called needs_parsing or similar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree!

if (any(parse_ids)) {
graticule$degree_label <- Map(
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather not use Map() here - I suspect you could more simply right as something like:

labels <- as.list(graticule$degree_label)
labels[parse_ids] <- lapply(labels[parse_ids], parse_safe)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is much simpler. For some reason I always forget that we can assign to a subset of elements in a list. However, it still needs the part where only the first element of the expression object is extracted, so we get the element itself not inside an expression() statement:

labels <- as.list(graticule$degree_label)
labels[parse_ids] <- lapply(labels[parse_ids], function(x) parse_safe(x)[[1]])

Copy link
Member

Choose a reason for hiding this comment

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

Oh but parse_safe is already vectorised so you can skip the lapply

Copy link
Member

Choose a reason for hiding this comment

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

If you apply the character vector that hasn’t been coerced to a list

Copy link
Member

@clauswilke clauswilke Sep 4, 2018

Choose a reason for hiding this comment

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

Not sure. It may be best to work with a full reprex. The following works:

parse_safe <- function(text) {
  out <- vector("expression", length(text))
  for (i in seq_along(text)) {
    expr <- parse(text = text[[i]])
    out[[i]] <- if (length(expr) == 0) NA else expr[[1]]
  }
  out
}

graticule <- data.frame(
  type = c("E", "E", "E", "E"),
  degree_label = c(NA, "abcd", "10 * degree * E", "15 * degree * E"),
  stringsAsFactors = FALSE
)

needs_parsing <- grepl("degree", graticule$degree_label)
labels <- as.list(graticule$degree_label)
labels[needs_parsing] <- lapply(labels[needs_parsing], function(x) parse_safe(x)[[1]])
graticule$degree_label <- labels
graticule
#>   type    degree_label
#> 1    E              NA
#> 2    E            abcd
#> 3    E 10 * degree * E
#> 4    E 15 * degree * E

Do you see a way to make it simpler?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you're correct, it can be made simpler.

parse_safe <- function(text) {
  out <- vector("expression", length(text))
  for (i in seq_along(text)) {
    expr <- parse(text = text[[i]])
    out[[i]] <- if (length(expr) == 0) NA else expr[[1]]
  }
  out
}

graticule <- data.frame(
  type = c("E", "E", "E", "E"),
  degree_label = c(NA, "abcd", "10 * degree * E", "15 * degree * E"),
  stringsAsFactors = FALSE
)

needs_parsing <- grepl("degree", graticule$degree_label)
labels <- as.list(graticule$degree_label)
labels[needs_parsing] <- parse_safe(graticule$degree_label[needs_parsing])
graticule$degree_label <- labels
graticule
#>   type    degree_label
#> 1    E              NA
#> 2    E            abcd
#> 3    E 10 * degree * E
#> 4    E 15 * degree * E

Created on 2018-09-04 by the reprex package (v0.2.0).

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that’s exactly what I was imagining.

function(parse_id, label) {
if (parse_id) {
parse(text = label)[[1]]
} else {
as.expression(label)[[1]]
Copy link
Member

Choose a reason for hiding this comment

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

Why are we turning label into an expression object and then immediately extracting it out of the expression object?

Copy link
Member

Choose a reason for hiding this comment

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

Because we need the symbol itself, not expression(symbol). This is directly copied from the current code.

Copy link
Member

Choose a reason for hiding this comment

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

One problem with this code chunk as a whole, is that when I read it I can't easily tell what type of object graticule$degree_label is supposed to be. It's obviously a list, and some of the components are expressions (not expression objects), but what are the others supposed to be?

You could fix that with a comment, but it would be even better to use functions where it's obvious what the output type is.

Copy link
Member

Choose a reason for hiding this comment

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

If we need a symbol, and we have a character vector of length one, then the function to use is as.symbol()

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I was confused. This can definitely be simplified.
as.expression(label)[[1]] came from an earlier version of the code where I stuck everything into an expression object and thus needed as.expression(label). But now everything goes into a list, and strings can go directly since they are self-quoting.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, so my gut feeling was right and the code I suggest below is a bit clearer.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, correct. See also my other comment, below.

}
},
parse_ids, graticule$degree_label
)
}

graticule
},
Expand Down
18 changes: 18 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,21 @@ is_column_vec <- function(x) {
dims <- dim(x)
length(dims) == 2L && dims[[2]] == 1L
}

# Parse takes a vector of n lines and returns m expressions.
# See https://github.com/tidyverse/ggplot2/issues/2864 for discussion.
#
# parse(text = c("alpha", "", "gamma"))
# #> expression(alpha, gamma)
#
# parse_safe(text = c("alpha", "", "gamma"))
# #> expression(alpha, NA, gamma)
#
parse_safe <- function(text) {
out <- vector("expression", length(text))
for (i in seq_along(text)) {
expr <- parse(text = text[[i]])
out[[i]] <- if (length(expr) == 0) NA else expr[[1]]
}
out
}
49 changes: 49 additions & 0 deletions tests/testthat/test-utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,52 @@ test_that("find_args behaves correctly", {
# Defaults are overwritten
expect_true(test_fun(arg2 = TRUE)$arg2)
})

test_that("parse_safe works with simple expressions", {
expect_equal(
parse_safe(c("", " ", " ")),
expression(NA, NA, NA)
)

expect_equal(
parse_safe(c("A", "B", "C")),
expression(A, B, C)
)

expect_equal(
parse_safe(c("alpha", "", "gamma", " ")),
expression(alpha, NA, gamma, NA)
)

expect_equal(
parse_safe(c(NA, "a", NA, "alpha")),
expression(NA, a, NA, alpha)
)

expect_equal(
parse_safe(factor(c("alpha", "beta", ""))),
expression(2, 3, 1)
)
})

test_that("parse_safe works with multi expressions", {
expect_equal(
parse_safe(c(" \n", "\n ", " \n \n ")),
expression(NA, NA, NA)
)

expect_equal(
parse_safe(c("alpha ~ beta", "beta \n gamma", "")),
expression(alpha ~ beta, beta, NA)
)

expect_equal(
parse_safe(c("alpha ~ beta", " ", "integral(f(x) * dx, a, b)")),
expression(alpha ~ beta, NA, integral(f(x) * dx, a, b))
)

expect_equal(
parse_safe(c(NA, 1, 2, "a \n b")),
expression(NA, 1, 2, a)
)
})