Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R function with no return value

Tags:

function

r

I was helping a friend of mine with some of his code. I didn't know how to explain the strange behavior, but I could tell him that his functions weren't explicitly returning anything. Here is a minimum reproducible example:

derp <- function(arg){
  arg <- arg+3
}

data <- derp(500)
data
#[1] 503
derp(500)
#nothing outputs
class(derp(500))
#[1] "numeric"

Is there a name for this that I can google? Why is this happening? Why isn't arg being destroyed after the call to derp() finishes?

like image 576
Taylor Avatar asked Feb 04 '14 00:02

Taylor


4 Answers

You need to understand the difference between a function returning a value, and printing that value. By default, a function returns the value of the last expression evaluated, which in this case is the assignment

arg <- arg + 3

(Note that in R, an assignment is an expression that returns a value, in this case the value assigned.) This is why data <- derp(500) results in data containing 503.

However, the returned value is not printed to the screen by default, unless you isolate the function's final expression on its own line. This is one of those quirks in R. So if you want to see the value:

derp <- function(arg)
{
    arg <- arg + 3
    arg
}

or just

derp <- function(arg)
arg + 3
like image 75
Hong Ooi Avatar answered Nov 09 '22 06:11

Hong Ooi


I often use return(NULL) for in the beginning of a function enclosed in some consistency checks, e.g. whether an output file would be overwritten. e.g.

some_function <- function(output_filename, overwrite = FALSE) {
  if (file.exists(output_filename)) {
    if (!overwrite) {
      return(NULL)
    }
  }
}

In this case the calling some_function("file.path", overwrite = FALSE) would return NULL. You can prevent that by modifying the return to:

...
return(invisible(NULL))
...
like image 29
loki Avatar answered Sep 30 '22 18:09

loki


the arg variable is being destroyed. A function in R will return the value of the last statement executed in the function unless a return statement is explicitly called.

In your case a copy of arg is the return value of your function. Example:

alwaysReturnSomething = function()
{
  x = runif(1)
  if(x<0.5) 20  else 10
}
> for(x in 1:10) cat(alwaysReturnSomething())
20202020102010101020

or:

alwaysReturnSomething <- function(){}
> z=alwaysReturnSomething()
> z
NULL
like image 1
Hans Roggeman Avatar answered Nov 09 '22 06:11

Hans Roggeman


This is curious behavior.

Basically derp(), it returns if you assign the output of derp(), and derp() does not return if you do not assign the result. This is because the assignment function (<-) returns using the invisible() function. see Make a function return silently for how that works.

You can see the same behavior with derp2:

derp2 <- function(arg) {
  invisible(arg + 3)
}
derp2(3)
# nothing
b <- derp2(3)
b
# 6
like image 1
pdb Avatar answered Nov 09 '22 06:11

pdb