R function with no return value

后端 未结 5 421
滥情空心
滥情空心 2020-12-03 22:04

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 returni

相关标签:
5条回答
  • 2020-12-03 22:30

    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))
    ...
    
    0 讨论(0)
  • 2020-12-03 22:34

    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
    
    0 讨论(0)
  • 2020-12-03 22:44

    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
    
    0 讨论(0)
  • 2020-12-03 22:49

    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
    
    0 讨论(0)
  • 2020-12-03 22:49

    If you want a function to change the value of a variable in Global environment you can use <<- operator, but you still need to write the exact name of a variable in Global environment

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

    Try it and call b

    0 讨论(0)
提交回复
热议问题