[R] Question about expression parser for "return" statement

Duncan Murdoch murdoch.duncan at gmail.com
Sun Nov 13 19:35:57 CET 2016

On 13/11/2016 7:58 AM, Duncan Murdoch wrote:
> On 13/11/2016 6:47 AM, Duncan Murdoch wrote:
>> On 13/11/2016 12:50 AM, Dave DeBarr wrote:
>>> I've noticed that if I don't include parentheses around the intended return
>>> value for the "return" statement, R will assume the first parenthetical
>>> expression is the intended return value ... even if that parenthetical
>>> expression is only part of a larger expression.
>>> Is this intentional?
>> Yes, return is just a function call that has side effects.  As far as
>> the parser is concerned,
>> return ((1/sqrt(2*pi*Variance))*exp(-(1/2)*((x - Mean)^2)/Variance))
>> is basically the same as
>> f((1/sqrt(2*pi*Variance))*exp(-(1/2)*((x - Mean)^2)/Variance))
> By the way, out of curiosity I took a look at the source of CRAN
> packages to see if this actually occurs.  It turns out that "return" is
> used as a variable name often enough to make automatic tests tricky, so
> I don't know the answer to my question.  However, I did turn up a number
> of cases where people have code like this:
>      if (name == "") return;
> (from the bio.infer package), which never calls return(), so doesn't
> actually do what the author likely intended

I searched the R sources and the sources of CRAN packages, and found 
this is a reasonably common problem:  it's in 111 packages, including 
one in base R.  I'll be emailing the maintainers to let them know.

I'll see about putting a check for this into R CMD check.

Duncan Murdoch

More information about the R-help mailing list