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

Add functions for checking plot labels #22

Merged
merged 6 commits into from
Nov 18, 2021
Merged

Add functions for checking plot labels #22

merged 6 commits into from
Nov 18, 2021

Conversation

rossellhayes
Copy link
Contributor

Adds function get_labels() for listing (a subset of) the labels of a plot, and uses_labels() for checking if labels match their expected values.

library(ggcheck)
library(ggplot2)

p <- ggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  labs(x = "weight", y = "MPG", color = NULL)

get_labels(p)
#> $x
#> [1] "weight"
#> 
#> $y
#> [1] "MPG"
#> 
#> $colour
#> NULL

get_labels(p, c("x", "y"))
#> $x
#> [1] "weight"
#> 
#> $y
#> [1] "MPG"

uses_labels(p, x = "weight")
#> [1] TRUE

uses_labels(p, color = NULL)
#> [1] TRUE

# If multiple arguments are given, returns TRUE only if all labels match
uses_labels(p, x = "weight", color = NULL)
#> [1] TRUE

Created on 2021-11-12 by the reprex package (v2.0.1)

@rossellhayes rossellhayes added the enhancement New feature or request label Nov 12, 2021
@rossellhayes rossellhayes self-assigned this Nov 12, 2021
Copy link

@deirdrefrancks deirdrefrancks left a comment

Choose a reason for hiding this comment

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

Looks good to me!

R/labels.R Outdated
Comment on lines 84 to 86
!all(vapply(args, length, integer(1)) <= 1) ||
!all(vapply(args[lengths(args) > 0], is.character, logical(1))) ||
!all(vapply(args[lengths(args) == 0], is.null, logical(1)))
Copy link
Member

Choose a reason for hiding this comment

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

Let's use rlang or refactor this into a helper function or intermediate variables

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 7a5677c

Copy link
Member

@gadenbuie gadenbuie left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants