<- function(x){
add_two + 2
x }
Today we will…
Functions allow you to automate common tasks!
Writing functions has three big advantages over copy-paste:
Let’s define a function.
add_two
The name of the function is chosen by the author.
The argument(s) of the function are chosen by the author.
If we supply a default value when defining the function, the argument is optional when calling the function.
{ }
The body of the function is where the action happens.
return()
Your function will give back what would normally print out…
return()
sI tend to disagree…
I prefer to use return()
statements because it saves me from accidentally writing a function that outputs nothing.
return()
If you need to return more than one object from a function, wrap those objects in a list.
When a function requires an input of a specific data type, check that the supplied argument is valid.
add_something <- function(x, something){
if(!is.numeric(x)){
stop("Please provide a numeric input for the x argument.")
}
return(x + something)
}
add_something(x = "statistics", something = 5)
Error in add_something(x = "statistics", something = 5): Please provide a numeric input for the x argument.
How would you modify the previous code to validate both x
and something
?
Meaning, the function should check if both x
and something
are numeric.
add_something <- function(x, something){
if(!is.numeric(x) | !is.numeric(something)){
stop("Please provide numeric inputs for both arguments.")
}
return(x + something)
}
add_something(x = 2, something = "R")
Error in add_something(x = 2, something = "R"): Please provide numeric inputs for both arguments.
The location (environment) in which we can find and access a variable is called its scope.
We cannot access variables created inside a function outside of the function.
Name masking occurs when an object in the function environment has the same name as an object in the global environment.
Functions look for objects FIRST in the function environment and SECOND in the global environment.
It is not good practice to rely on global environment objects inside a function!
You will make mistakes (create bugs) when coding.
When you have a concept that you want to turn into a function…
Write a simple example of the code without the function framework.
Generalize the example by assigning variables.
Write the code into a function.
Call the function on the desired arguments
This structure allows you to address issues as you go.
Write a function called find_car_make()
that takes in the name of a car and returns the “make” of the car (the company that created it).
find_car_make("Toyota Camry")
should return “Toyota”.find_car_make("Ford Anglica")
should return “Ford”.Taking these examples, write a find_car_make()
function that will output the maker / manufacturer of any car.
Once you’ve got a function written, can you add input validation to your function?
find_car_make()
find_car_make <- function(car_name){
# Check if there is a space in the string (so there are separate parts)
stopifnot(
stringr::str_detect(car_name, pattern = "\\w")
)
# Grab the first word of the car name (assuming Toyota Camry style input)
make <- word(string = car_name,
start = 1,
end = 1)
return(make)
}
You will write several small functions, then use them to unscramble a message. Many of the functions have been started for you, but none of them are complete as is.
This activity will require knowledge of:
if ()
& else if()
statements[]
and logical values to extract elements of a vector
None of us have all these abilities. Each of us has some of these abilities.
Every group should have a base R cheatsheet!
On the front middle:
[]
to extract elements of a vectorOn the front right:
The partner who lives the furthest from SLO starts as the Developer (typing and listening to instructions from the Coder)!
Submit the name of the television show the six numbers are asssociated with.
PA-7-functions.html
file for the group.