Skip to content

Instantly share code, notes, and snippets.

@ClaytonJY
Created November 9, 2018 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClaytonJY/d88a234df58267328f676d2041c637d6 to your computer and use it in GitHub Desktop.
Save ClaytonJY/d88a234df58267328f676d2041c637d6 to your computer and use it in GitHub Desktop.
default args for bare col names: missing or NULL?
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