Created
November 9, 2018 15:46
-
-
Save ClaytonJY/d88a234df58267328f676d2041c637d6 to your computer and use it in GitHub Desktop.
default args for bare col names: missing or NULL?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(rlang) | |
f <- function(df, x, y, facet) { | |
x <- enquo(x) | |
y <- enquo(y) | |
facet = enquo(facet) | |
g <- ggplot(df, aes(x = !!x, y = !!y)) + | |
geom_point() | |
if (!quo_is_missing(facet)) g <- g + facet_wrap(vars(!!facet)) | |
g | |
} | |
f(mtcars, mpg, hp) | |
f(mtcars, mpg, hp, cyl) | |
f2 <- function(df, x, y, facet = NULL) { | |
x <- enquo(x) | |
y <- enquo(y) | |
facet = enquo(facet) | |
g <- ggplot(df, aes(x = !!x, y = !!y)) + | |
geom_point() | |
if (!is.null(quo_get_expr(facet))) g <- g + facet_wrap(vars(!!facet)) | |
g | |
} | |
f2(mtcars, mpg, hp) | |
f2(mtcars, mpg, hp, cyl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment