Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher
Last active August 14, 2019 21:03
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 dirkschumacher/5cedf86414949156a7d0970b88326f96 to your computer and use it in GitHub Desktop.
Save dirkschumacher/5cedf86414949156a7d0970b88326f96 to your computer and use it in GitHub Desktop.
`?` <- 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