Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher
Created February 8, 2019 19:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkschumacher/944ce2877316fe8c1fe8b7d21fcc248c to your computer and use it in GitHub Desktop.
Save dirkschumacher/944ce2877316fe8c1fe8b7d21fcc248c to your computer and use it in GitHub Desktop.
splice_df <- function(x, ...) {
  expr <- rlang::enquo(x)
  cols <- lapply(rlang::ensyms(..., .named = TRUE), as.character)
  lapply(cols, function(col_name) {
    rlang::quo(`[[`(!!expr, !!col_name))
  })
}
dplyr::summarise(mtcars,
  n = n(),
  x = sum(hp > 100),
  !!!splice_df(binom::binom.wilson(x, n), mean, lower, upper)
)
##    n  x    mean     lower     upper
## 1 32 23 0.71875 0.5462549 0.8443542
rlang::quo(
  dplyr::summarise(mtcars,
    n = n(),
    x = sum(hp > 100),
    !!!splice_df(binom::binom.wilson(x, n), mean, lower, upper)
  )
)
## <quosure>
## expr: ^dplyr::summarise(mtcars, n = n(), x = sum(hp > 100), mean = ^^binom
##         ::binom.wilson(x, n)[["mean"]], lower = ^^binom::binom.wilson(x,
##           n)[["lower"]], upper = ^^binom::binom.wilson(x, n)[["upper"]])
## env:  global
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment