6.1 Introduction
If you’re reading this book, you’ve probably already created many R functions and know how to use them to reduce duplication in your code. In this chapter, you’ll learn how to turn that informal, working knowledge into more rigorous, theoretical understanding. And while you’ll see some interesting tricks and techniques along the way, keep in mind that what you’ll learn here will be important for understanding the more advanced topics discussed later in the book.
Quiz
Answer the following questions to see if you can safely skip this chapter. You can find the answers in Section 6.9.
What are the three components of a function?
What does the following code return?
10 x <- function(x) { f1 <-function() { + 10 x } }f1(1)()
How would you usually write this code?
`+`(1, `*`(2, 3))
How could you make this call easier to read?
mean(, TRUE, x = c(1:10, NA))
Does the following code throw an error when executed? Why or why not?
function(a, b) { f2 <-* 10 a }f2(10, stop("This is an error!"))
What is an infix function? How do you write it? What’s a replacement function? How do you write it?
How do you ensure that cleanup action occurs regardless of how a function exits?
Outline
Section 6.2 describes the basics of creating a function, the three main components of a function, and the exception to many function rules: primitive functions (which are implemented in C, not R).
Section 6.3 discusses the strengths and weaknesses of the three forms of function composition commonly used in R code.
Section 6.4 shows you how R finds the value associated with a given name, i.e. the rules of lexical scoping.
Section 6.5 is devoted to an important property of function arguments: they are only evaluated when used for the first time.
Section 6.6 discusses the special
...
argument, which allows you to pass on extra arguments to another function.Section 6.7 discusses the two primary ways that a function can exit, and how to define an exit handler, code that is run on exit, regardless of what triggers it.
Section 6.8 shows you the various ways in which R disguises ordinary function calls, and how you can use the standard prefix form to better understand what’s going on.