Previous chapter
Writing FunctionsArguments
Next chapter

Matching Quiz

Assigning the right values to the right arguments can make or break your code.

log(1, 2)
## [1] 0
log(2, 1)
## [1] Inf
## Uh oh.

But how much do you know about assigning arguments? Take this quiz to find out.

methods

Not matching

Consider this brilliant function:

foo <- function(aaa, abb, abc) {
  c(aaa, abb, abc)
}

Default values

Let’s make foo even more brilliant:

foo <- function(aaa = 1, abb = 2, abc = 3) {
  c(aaa, abb, abc)
}

Position

Remember foo:

foo <- function(aaa = 1, abb = 2, abc = 3) {
  c(aaa, abb, abc)
}

foo <- function(aaa = 1, abb = 2, abc = 3) {
  c(aaa, abb, abc)
}

Names

foo <- function(aaa = 1, abb = 2, abc = 3) {
  c(aaa, abb, abc)
}

Partial names

foo <- function(aaa = 1, abb = 2, abc = 3) {
  c(aaa, abb, abc)
}

foo <- function(aaa = 1, abb = 2, abc = 3) {
  c(aaa, abb, abc)
}

Dots

Now for some dark magic:

foodoo <- function(...) {
  c(...)
}

foodoo <- function(...) {
  c(...)
}

Here is one last wrinkle. Suppose we rewrite foodoo slightly:

foodoo <- function(a, ...) {
  c(...)
}

Tips

Congratulations! You’ve finished the quiz and know a lot about how R parses arguments. As a reward, here are two useful things that you can do with arguments in the RStudio IDE:

After you’ve written a function name followed by an opening parenthesis, look for a small yellow window to pop up that tells you the names, order, and default values of the function’s arguments. This can be handy when you are not very familiar with the function.

After you’ve written a function name followed by an opening parenthesis, press tab to invoke RStudio’s tab completion. RStudio will open a pop up menu that contains the names of the function’s arguments. Use the up and down arrow keys to navigate to a name, and then press enter to insert the name into your code. This can save you a lot of time once you get a feel for it.