#' Arbitrary function#'#'@description#' `function_name` is an example function to highlight function syntax details.#' This function calls `other_function` as part of its implementation#' The documentation style is part of the roxygen package#'#'@param argument1 Description of the first argument (with it's type).#'@param argument2 Description of the second argument (with it's type).#'@param argument3 Description of the third argument (with it's type).#'@param ... Arbitrary additional arguments pass on to `other_function`.#'#'@returns Brief description of type (and e.g. shape) of what the function returnsfunction_name <-function(argument1, argument2, argument3 ='default_value', ...){# Function body# `...` are arbitrary arguments that can be passed on to other functions some_val <-other_function(argument1, ...)return(return_value)}
Using (AKA calling) a (named) function
# Positional arguments onlyvalue <-function_name(value1, value2)# Combining positional and named arguments (in arbitrary order)value <-function_name(argument3 ='other_value', value1, value2)# Adding arbitrary arguments that will be passed on # (only if function definition allows it)value <-function_name( value1, # goes to argument1 value2, # goes to argument2 value3, # goes to argument3 arbitrary_value4, # goes to ..., so (in this case) gets passed on to `other_function`)
Anonymous functions
# Some functions take other functions as arguments, these other function can be # supplied in-line and are called 'anonymous' functionsvalue <-some_function( input, function(el) {# ...do something with el here# Note that `some_function` determines how this anonymous function will be called })# Examplesapply(1:4, function(i){ i**2 })# This returns: [1] 1 4 9 16
Control flow
Conditional execution
# Classic if/else statement (val is a single value)if (val >1) {# Do something} else {# Do something else}# Vectorized version (val is a vector)# new_val will be a vector with values 'large' and 'small'new_val <-ifelse(val >1, 'large', 'small')
For loops
for (i in x) {# do something with element i from collection x}
While loops
while (condition){# Do something while condition evaluates to TRUE# Stops when `condition` changes, when `break` is used, or (when inside a function)# when a `return` statement is usedif (other_condition){break }}
Repeat loops
repeat {# Do something repeatedly# When not in a function body, the only way to stop this is by using `break`# Inside a function body, a `return` statement also terminates the loopif (condition) {break }}
The apply functions
Error handling
# If no error occurs, `try` returns the normal return value, otherwise an error objectresult_or_error <-try(some_function_call(arg1, arg2))# `tryCatch` provides more fine grained control over what to do with errors or warningsresult <-tryCatch(function_call(arg1),error =function(e){# Optionally do something with the error object `e`return('Something to return on error') },warning =function(w){# Optionally do something with the warning object `w`return('Something to return on warning') })