From @zwj|08 @end|ng |rom gm@||@com  Mon May 27 04:27:09 2019
From: @zwj|08 @end|ng |rom gm@||@com (King Jiefei)
Date: Sun, 26 May 2019 22:27:09 -0400
Subject: [Rd] "if" function in pure R?
In-Reply-To: <CAERMt4fyeJ293njYHCFVZhYpm1Y8NaU8NSZ+Q3OFY2jopLMUEg@mail.gmail.com>
References: <CAERMt4fyeJ293njYHCFVZhYpm1Y8NaU8NSZ+Q3OFY2jopLMUEg@mail.gmail.com>
Message-ID: <CAGiFhPOkbKMiSZtiLnpsnN2wMx1fb-fj5Ud569Fjbzds6UbcMw@mail.gmail.com>

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:

> Hi all,
>
> Could anyone refer to me to a good source to learn how to program a simple
> control-flow construct* in R, or provide me with a simple example?
>
> Control-flow constructs are programmed as primitives, but I would like to
> be able to do that (if possible) in pure R.
>
> The general context is that those functions are a mystery to me. The
> motivating example is that I would like to create a function that behave
> similarly to base::`if` with an extra argument to the function (e.g. to
> include an error rate on the condition).
>
> Many thanks,
>
> Alex
>
> * control-flow constructs are functions such as if, for, while... that
> allow for call of the form fn(x) expr to work (see ?Control).
>
> --
> Alexandre Courtiol
>
> http://sites.google.com/site/alexandrecourtiol/home
>
> *"Science is the belief in the ignorance of experts"*, R. Feynman
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



[Rd] "if" function in pure R?
King Jiefei