`?` <- 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)