[R] How to modify the body of a function?
Duncan Murdoch
murdoch.duncan at gmail.com
Mon Jul 28 02:14:49 CEST 2014
On 27/07/2014, 7:29 PM, MacQueen, Don wrote:
> As long as people are sharing their preferences . . .
>
>
> I find return() useful in a scenario like the following:
>
> Myfun <- function() {
> {a few lines of code}
> if (condition) return(whatever)
> {Many, many lines of code}
> Results
> }
>
> Which I find preferable to
>
> Myfun <- function() {
> { a few lines of code}
> if (condition) {
> Results <- something
> } else {
> {Many, many lines of code}
> Results <- something.else
> }
> Results
> }
>
I tend to agree with you, but wanted to point out a third possibility:
Myfun <- function() {
{ a few lines of code}
if (condition) {
something
} else {
{Many, many lines of code}
something.else
}
}
In some sense this is the most "R-like", but I like it the least.
Duncan Murdoch
>
> It is the presence of those many lines of code which separate the opening
> and closing brackets after the else that make the former easier to read
> and understand (again in my opinion).
>
> I guess this is more along the lines of exception handling.
>
> Also note that this is something of a special case; I don¹t in general
> advocate using return().
>
More information about the R-help
mailing list