Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Explore arrange implementation #12

Open
hadley opened this issue Nov 8, 2018 · 1 comment
Open

Explore arrange implementation #12

hadley opened this issue Nov 8, 2018 · 1 comment

Comments

@hadley
Copy link
Member

hadley commented Nov 8, 2018

Interesting because you embed quosures in an expression

arrange2 <- function(.df, ..., .na.last = TRUE) {
  # Capture all dots
  args <- enquos(...)
  
  # Uses `!!!` to splice in the individual arguments
  # Use `!!` to inline `.na.last` to avoid it being matched in the data mask
  order_call <- expr(order(!!!args, na.last = !!.na.last))
  
  # Evaluate the call to order using data mask
  ord <- eval_tidy(order_call, .df)
  stopifnot(length(ord) == nrow(.df))
  
  .df[ord, , drop = FALSE]
}

df <- data.frame(x = c(2, 3, 1), y = runif(3))

arrange2(df, x)
arrange2(df, -y)
@hadley
Copy link
Member Author

hadley commented Nov 12, 2018

Compare and contrast with

arrange3 <- function(.data, ..., .na.last = TRUE) {
  args <- enquos(...)
  
  ords <- purrr::map(args, eval_tidy, data = .data)
  ord <- exec(order, !!!ords, na.last = .na.last)
  
  .data[ord, , drop = FALSE]
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant