# Wrapping R vectors with parentheses

In this vectors and lists tutorial, the example code wraps the R vector assignments with parentheses or round brackets.

(v_log <- c(TRUE, FALSE, FALSE, TRUE))
#> [1]  TRUE FALSE FALSE  TRUE
(v_int <- 1:4)
#> [1] 1 2 3 4
(v_doub <- 1:4 * 1.2)
#> [1] 1.2 2.4 3.6 4.8
(v_char <- letters[1:4])
#> [1] "a" "b" "c" "d"

The purpose (I believe) is simply to perform the assignments and to output the vector, so the reader can see what's in the vectors. I wanted to find confirmation of this but my search engine skills yielded nothing conclusive. Perhaps it's so obvious (and/or unimportant) that no one has bothered to write up an explanation. But I'd still like one and here's my short post on what wrapping R vectors with parentheses does.

One of the pages I came across while looking up R parentheses was the Parentheses and Braces documentation page and below I show the description in verbatim:

Open parenthesis, (, and open brace, {, are .Primitive functions in R.

Effectively, ( is semantically equivalent to the identity function(x) x, whereas { is slightly more interesting, see examples.

Primitive functions (in case you were wondering) are:

Primitive functions are only found in the base package, and since they operate at a low level, they can be more efficient (primitive replacement functions don’t have to make copies), and can have different rules for argument matching (e.g., switch and call). This, however, comes at a cost of behaving differently from all other functions in R. Hence the R core team generally avoids creating them unless there is no other option.

Back to the Parentheses and Braces description. I interpreted the sentence:

( is semantically equivalent to the identity function(x) x

as ( simply being another (shorter) way to write the identity function, which is a basic function that takes one argument and the only thing it does is return the argument value, i.e. function(x) x.

Therefore, whatever is included between parentheses is simply returned back. In the code below, the assignment to v_log is performed and supplied to the identity function, which returns it back and is shown as output.

(v_log <- c(TRUE, FALSE, FALSE, TRUE))
#> [1]  TRUE FALSE FALSE  TRUE

Since whatever supplied will be returned, this works on other types.

On a matrix.

(M <- matrix(1:9, nrow = 3))
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

On a function. The function test is returned and to actually call the function we need (), which is supplied at the end.

(test <- function(x) "Hi")()
[1] "Hi"

In conclusion, wrapping parentheses around code in R is a shortcut for calling the identity function function(x) x and is useful in tutorials since it saves you from typing another line of code for displaying the contents of your assignment. Or I could be completely wrong about all of this and if you may, please let me know the correct mechanism and purpose of wrapping parentheses around R code.