Quote While the Promise Is Hot!

October 18, 2018

Suppose we want to quote x when x is not NULL. The naive implementation would be like below. Here, y is for comparison. Do you understand why x and y are quoted differently? quote_x_and_y <- function(x, y) { if (is.null(x)) { stop("x is NULL!", call. = FALSE) } x <- rlang::enquo(x) y <- rlang::enquo(y) list(x, y) } x <- y <- 1 quote_x_and_y(x, y) #> [[1]] #> <quosure> #> expr: ^1 #> env: empty #> #> [[2]] #> <quosure> #> expr: ^y #> env: global This is because x is evaluated when is. ... Read more

dplyr::select() Accepts Characters Since Version 0.7.0

October 25, 2017

Some of my friends didn’t aware that dplyr now accepts characters. Did you? For example, this expression select(iris, Sepal.Length, Petal.Length) can be also written in this way: select(iris, "Sepal.Length", "Petal.Length") or in this way: select(iris, c("Sepal.Length", "Petal.Length")) For the semantics of select(), you can find a good explanation in the vignette. Tidyeval? You may want to write some code that selects columns programmatically using variables. x <- c("col1", "col2") select(some_data, x) But, be fareful, x can be ambiguous. ... Read more