Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher

dirkschumacher/q.md

Last active Aug 14, 2019
Embed
What would you like to do?
`?` <- function(lhs, rhs) {
  if (missing(rhs)) {
    return(eval(bquote(utils::`?`(.(substitute(lhs))))))
  }
  rhs <- substitute(rhs)
  envir <- parent.frame()
  split_colon <- strsplit(deparse(rhs), ":")
  stopifnot(length(split_colon) == 1L, length(split_colon[[1L]]) == 2L)
  rhs_chr <- split_colon[[1L]]
  branch_idx <- if (lhs) {
    1L  
  } else {
    2L
  }
  eval(parse(text = rhs_chr[branch_idx])[[1L]], envir = envir)
}

a <- 5

a == 1 ? 1 + a * 10^2 + 1 : a + 1
#> [1] 6

a == 5 ? 1 + a * 10^2 + 1 : a + 1
#> [1] 502

?strsplit

Created on 2019-08-14 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.