x <- 10Controlling flow
Recognize the value of functions as chunks of code that are reusable and easier to debug.
Conditionals in functions are used to modify how a function runs, determine what output to return or to return warnings and errors depending on logical statements. We’ll look at if and else, as well as functions for combining multiple logical statements. if and else can be used instead of stopifnot, usually in cases where logical statements are more complex or a more elaborate error message is required.
The following functions are essential for controlling flow in R:
-
ifelse(andelse if) -
||,&&combine multiple logical expressions and short-circuit when||sees the first true and&&sees the first false -
|,&are vectorized and cannot be directly used in anifstatement unless you also useany,all
Conditional Statements
Exercise: function conditionals
Write several if else statements.
Instruction: write a series of if else conditional statements for the following objects then consult the solution to check your work.
Numeric
- if x is a numeric, return x as a character
- print ‘positive’ if x is greater than 0, else print ‘negative’ if x is less than 0
- if x / 3 returns a remainder (hint: see
?`%%`), print the remainder
Show solution
data.frame
- return the column ‘colors’ if it exists in DF
- if type is ‘histogram’, plot a histogram of DF’s column ‘numbers’
DF <- data.frame(colors = c('red', 'green', 'blue'), numbers = c(42.1, 2L, 10))
type <- 'histogram'Exercise: conditional counts
Process data in a function.
Next we’ll write a new function that processes our prepared CSV files.
Let’s say we wanted to summarize the number of adults and chicks counted by island. First, write a function to sum the ‘adults’ and ‘chicks’ columns in the data.frame. Use the example data that we prepared with our prepare_csv function.
Follow the steps from our approach to developing functions:
- Setup the function
- Make a function script in the
R/directory namedsum_counts.R - Write the function’s skeleton (name, arguments, curly braces)
- Make a function script in the
- Setup the test script
- Make a corresponding test script in the
tests/directory namedtest_sum_counts.R. - Load any required packages (
library(package)) - Source the function (
source('R/function.R)) - Load example data and/or arguments for the function
- Make a corresponding test script in the
Since we have already built a function for preparing CSV files and filtering the prepared dataset, use them in your test script:
Add a new section ‘Development’ in the test script (tests/test_sum_counts.R) to develop the body of your function. Use the prepared and filtered CSV data as shown above, add the code to your function sum_counts() and test!
Hints: by
{data.table} has an argument by
{dplyr} has an alternative to group_by for passing character column names called group_by_at
Next steps:
- decide if it is better for you (or a potential user) to set the columns that we calculate the sum by inside the function (fixed) or as an argument (flexible) - try both options
- add a filter argument
filter_gt_adults(default set to NULL) and when it is not NULL, filter the data only greater than the value provided
Test these as you go in your test script tests/test_sum_counts.R.
Hints: defaults
To set a function’s argument to default, use the syntax argument = default_value. For example:
function(x = 1, y = 2, z = 3) {
}Bonus
- add checks to your function
sum_counts() - build a plot function that plots the output of
sum_counts()calledplot_xy() - add
x_colandy_colarguments to your plot function to dynamically update plot - use
x_colandy_colarguments to also update the axis labels inplot_xy()(base R) - use a new argument
color_colto provide a column inplot_xy()
