Home > Enterprise >  When to decide to call R functions with bracket vs without bracket
When to decide to call R functions with bracket vs without bracket

Time:01-30

I am learning R. Why is that sometime a function in R is called with brackets like say myfunction() vs sometimes it is called without myfunction

How do I know when to call with bracket vs without?

I see a lot of function calls in tidyverse without a bracket

CodePudding user response:

In R you can think of a function as an object. The object has two components: the arguments that the function accepts (called the formals), and the actual code inside the curly brackets called the function body.

For example, lets define a simple function called f:

f <- function(x) { x   1 }

If we just type f with no parentheses into the console, we can see the formals and body of this new object f:

f
#> function(x) { x   1 }
#> <bytecode: 0x000001f8dc3e3f50>

We can see its formals separately by doing:

formals(f)
#> $x

And its body by doing:

body(f)
#> {
#>     x   1
#> }

So the function is really just an object. But normally we want to use the function to do some calculation for us. We use a function by calling it. That means that we give some values to the arguments in the formals, and these are used in the calculations inside the body.

When we call a function, we normally do it by putting our variables in parentheses after the function's name:

f(1)
#> [1] 2

But this isn't the only way to call a function. We can give R the function name and the arguments we wish to pass to it in do.call for example:

do.call(f, list(x = 1))
#> [1] 2

We can even build the call as a list of function name and argument, then ask R to evaluate it directly:

eval(as.call(list(f, x = 1)))
#> [1] 2

But the most common way is to simply put parentheses with the arguments after our function name. The R parser recognizes this as meaning "call that function with these arguments". Note that the function doesn't even need a name for this to work:

(function(x){ x   1 })(1)
#> [1] 2

The reason we sometimes see functions without parentheses after is down to the fact that functions are objects which can themselves be passed to other functions. For example, suppose we have this function:

call_func_with_data <- function(func, data) {
  do.call(func, list(data))
}

I can pass any function I want into it and it will try to call it with the data I pass:

z <- 1:10

call_func_with_data(mean, z)
#> [1] 5.5
call_func_with_data(min, z)
#> [1] 1
call_func_with_data(max, z)
#> [1] 10
call_func_with_data(f, z)
#>  [1]  2  3  4  5  6  7  8  9 10 11

Note that I have used all these functions without parentheses. This is in essence what is happening inside tidyverse and apply-type functions where you see function names without parentheses after them. It is also what is happening when you use the syntax:

1 %>% f
#> [1] 2

These are just different ways of calling the function.

Created on 2022-01-29 by the reprex package (v2.0.1)

CodePudding user response:

There are really only two possibilities:

  1. Call the function by putting parentheses after it, e.g. sqrt(4)

  2. Pass the function itself to another function which in turn invokes it. In this case we are not calling the function but passing it on as an argument to another function. e.g.

     sapply(1:4, sqrt) # pass sqrt to sapply which invokes it repeatedly
     do.call(sqrt, list(4))  # pass sqrt to do.call which in turn invokes it
    

infix

There are also infix functions such as but these are just different on the surface and are actually the same underneath. For example, these are the same.

   1 2
   ` `(1, 2)

pipes

There are also pipes but these just manipulate the syntax so that either of these are conceptually the same.

 sqrt(4)
 4 |> sqrt()

The magrittr package also has pipes so any of these are conceptually the same. Here it is passing sqrt or sqrt() to the %>% function which then interprets it as if it were the first sqrt line below.

library(magrittr)

sqrt(4)
4 %>% sqrt
4 %>% sqrt()
  •  Tags:  
  • Related