Skip to content
Prev 56896 / 63421 Next

"if" function in pure R?

Hi Alexandre,

I'm not an R expert so this is only my personal thought:

I don't think you can achieve what you want exactly. A possible solution
would be defining a binary operator %*%, where you can replace the asterisk
with any function name you want. The function %*% is special since it has
two arguments, left operand and right operand respectively. You then
can call the `substitute` function to get its function arguments in an
expression format and proceed to do what you want. Here is an example to
show the idea.

*Code:*

`%myOperator%` <- function(x, y) {
  x = substitute(x)
  y = substitute(y)
  return(list(x, y))
}


myIf(i == 1, arg1) %myOperator% {
  doSomeThing
}


*Results:*

[[1]]
myIf(i == 1, arg1)

[[2]]
{
    doSomeThing
}

I hope that helps.

Best,
Jiefei

On Sun, May 26, 2019 at 4:45 AM Alexandre Courtiol <
alexandre.courtiol at gmail.com> wrote: