%(% {commafree}R Documentation

Call a function

Description

This allows you to call a function with expressions for arguments. It is especially useful for long, multi-line function calls with many arguments (e.g., a shiny UI definition, an R6 class definition, ...)

Usage

fn %(% args

Arguments

fn

A function

args

A set of expressions grouped by { }

Details

This (⁠%(%⁠) merely performs a syntax transformation, so all the same semantics with regards to lazy argument evaluation apply. For any function call, replace ( with ⁠%(%⁠ and be free of the need for commas between arguments in that call.

fn %(% {
  a
  b
  c
}

Is syntactically equivalent to writing:

func(
  a,
  b,
  c
)

Value

Whatever fn() called with args returns.

Note

You can produce a missing argument with the special token ⁠,,⁠, or foo = `,` for a named missing arguments (see examples).

Examples

mean %(% {
  1:3
  na.rm = TRUE
}

writeLines(c %(% {
  "Hello"
  "Goodbye"
})

# setup helper demonstrating missing arguments
fn <- function(x, y) {
  if(missing(y))
    print("y was missing")
  else
    print(y)
}

# How to add a named missing argument
fn %(% {
  y = `,`
}

# How to add a positional missing argument
fn %(% {
  1
  `,,`
}

fn %(% { 1; `,,` }

rm(fn) # cleanup

[Package commafree version 0.2.0 Index]