tests/testthat/references/functions.R

# ------------------------------------------------------------------------------
# functions

function() 1
function() {}
function(arg1, arg2) {
  arg2
}

function(x, y) return(y)

function(arg1, arg2 = 2) {}

function(x,
         y,
         z = 3) {

}

function()


  1

function
() 1 + 1

function

() 1 + 1

function

# this important comment

() 1 + 1

function() function() {}

function(x = function() {}) {}

# With no intermediate `{` scope
function() for(i in 1:5) i

# ------------------------------------------------------------------------------
# function dots and dot dot i

# Dots as parameter without default
function(...) { list(...) }

# Dots as parameter with default
function(... = 1) { get("...") }

# `..i` as parameter without default
function(..1) { get("..1") }

# `..i` as parameter with default
function(..1 = 1) { get("..1") }

# Miscellaneous
function(x, ...) f(...)
function(x, ...) ..1 + ..2

# ------------------------------------------------------------------------------
# function no body

function(x, y)

# ------------------------------------------------------------------------------
# function no body with assignment

x <- function(x, y)

# ------------------------------------------------------------------------------
# function no body inside another function

function(x = function()) {}

# ------------------------------------------------------------------------------
# lambda function

a <- \(arg) arg
b <- \(arg1, arg2) paste0(arg1, arg2)
c <- \(fun, ...) fun(...)
1:3 |> {\(x, y = 1) x + y}() |> {\(x) sum(x)}()
{\(a = 1) a + 1}()
\() 1 + 2
\()

  1 + 2

# Not currently allowed by the parser, but we think it will be and is just an oversight.
# `'\\'` would need to be included alongside `FUNCTION` here https://github.com/wch/r-source/blob/802121c877837926a6bc2a930b3da749b537258b/src/main/gram.y#L3898-L3901.
\
() 1 + 1

Try the treesitter.r package in your browser

Any scripts or data that you put into this service are public.

treesitter.r documentation built on June 8, 2025, 2:03 p.m.