24 Functions

24.1 Function components

  1. Q: What function allows you to tell if an object is a function? What function allows you to tell if a function is a primitive function?
    A: You can test objects with is.function and is.primitive.

  2. Q: This code makes a list of all functions in the base package.

    Use it to answer the following questions:

    1. Which base function has the most arguments?

    2. How many base functions have no arguments? What’s special about those functions?

    3. How could you adapt the code to find all primitive functions?

    A:

    1. First we create a named vector that returns the number of arguments per function and then we subset it with the index of it’s maximum entry:
    1. We check the number of functions with formals() returning 0 or NULL. Then we will see, that all of these functions have formals equal to NULL, which means, that they should be primitive functions.
    Hence not all functions with formals equal to NULL are primitive functions, there must be non primitive functions with this property too.
    1. Change the predicate in Filter to is.primitive:
  3. Q: What are the three important components of a function?
    A: body(), formals() and environment().

    There is one exception to the rule that functions have three components. Primitive functions, like sum(), call C code directly with .Primitive() and contain no R code. Therefore their formals(), body(), and environment() are all NULL.

  4. Q: When does printing a function not show what environment it was created in?
    A: When it was created in the global environment.

24.2 Lexical Scoping

  1. Q: What does the following code return? Why? What does each of the three c’s mean?

    A: A named vector c, which first field has the value 10 and the name “c”. The first “c” is the c() function, the second is the name of the first entry and the third is the value of the first entry.

  2. Q: What are the four principles that govern how R looks for values?
    A: As stated in the book:

    There are four basic principles behind R’s implementation of lexical scoping:

    • name masking
    • functions vs. variables
    • a fresh start
    • dynamic lookup
  3. Q: What does the following function return? Make a prediction before running the code yourself.

    A: 202

24.3 Function arguments

  1. Q: Clarify the following list of odd function calls:

  2. Q: What does this function return? Why? Which principle does it illustrate?

    A: It returns 3 and illustrates lazy evaluation. As you can see, y becomes 1, but only when x is evaluated (before y) inside the function (otherwise it is 0):

    Note that funny things can happen if we switch the evaluation order (even within one line)

    or we evaluate y once before and once after the evaluation of x

  3. Q: What does this function return? Why? Which principle does it illustrate?

    A: 100, lazy evaluation.

24.4 Special calls

  1. Q: Create a list of all the replacement functions found in the base package. Which ones are primitive functions?
    A: We can find replacementfunctions by searching for functions that end on “<-”:

  2. Q: What are valid names for user-created infix functions?

    A:

    All user-created infix functions must start and end with % … they can contain any sequence of characters (except “%”, of course).

  3. Q: Create an infix xor() operator.
    A:

  4. Q: Create infix versions of the set functions intersect(), union(), and setdiff().
    A:

  5. Q: Create a replacement function that modifies a random location in a vector.
    A:

24.5 Return Values

  1. Q: How does the chdir parameter of source() compare to in_dir()? Why might you prefer one approach to the other? The in_dir() approach was given in the book as

    A: in_dir() takes a path to a working directory as an argument. At the beginning of the function the working directory is changed to this specification and with a call to on.exit it is guranteed, that when the function finishes the working directory also equals to this specification.

    In source() you need the chdir argument to specify, if the working directory should be changed during the evaluation to the file argument, if this is a pathname. The difference in source() is, that the actual working directory as output of getwd() is saved to set it in on.exit before changing the directory to the pathname (given to the file argument) for the rest of the execution of the source() function.

  2. Q: What function undoes the action of library()? How do you save and restore the values of options() and par()?
    A: Use detach() with "package:pacakgename" as first argument.
    Use, getOption() with "optionname" as argument. Type par() in the console and you will get a list with all actual settings.

  3. Q: Write a function that opens a graphics device, runs the supplied code, and closes the graphics device (always, regardless of whether or not the plotting code worked).
    A:

  4. Q: We can use on.exit() to implement a simple version of capture.output().

    Compare capture.output() to capture.output2(). How do the functions differ? What features have I removed to make the key ideas easier to see? How have I rewritten the key ideas to be easier to understand?

    A: Using body(capture.output), we can see the source code for the original capture.output() function. capture.output() is a good clip longer (39 lines vs. 7 lines). The reason for this is that capture.output2() is more modular, since capture.output() writes out entire methods like readLines() instead of invoking them. This makes capture.output2 easier to understand if you understand the underlying methods.

    However, capture.output2() does remove potentially important functionality, as capture.output() appears to handle important exceptions not handled in capture.output2(), and capture.output() offers the ability to chose between overwriting or appending to a file.