Answer
# create function #
<- function(x) {
double_it print(2 * x)
}
# try it out #
# explicit notation
double_it(x = 24)
# non explicit notation
double_it(24)
These exercises are adapted from Allison Horst’s EDS 221: Scientific Programming Essentials Course for the Bren School’s Master of Environmental Data Science program.
We’re going to start by creating some simple functions. Recall that the anatomy of a function is the same for all functions and each one contains:
{}
Create a function called double_it()
that doubles any value input value. Then try out the function, are the values returned what you expect?
# create function #
<- function(x) {
double_it print(2 * x)
}
# try it out #
# explicit notation
double_it(x = 24)
# non explicit notation
double_it(24)
Write a function called exclaim_age()
that returns the statement “I am ___ years old!”, where the blank is entered by the user as argument age.
Then try out the function, are the values returned what you expect?
# write function #
<- function(age) {
exclaim_age print(paste("I am", age, "years old!"))
}
# try it out #
# explicit notation
exclaim_age(age = 12)
# non explicit notation
exclaim_age(12)
Consider the function called find_max()
:
# "find_max() function" #
<- function(value_1, value_2) {
find_max
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
} }
# example using `find_max()` in an arithmetic expression #
# we expect the answer to be 20
5 * find_max(4, 2)
Run find_max(4, 2, 5)
in the Console.
What happens? What kind of message appears? Is it sufficient? If not, consider adding a warning or error message using warning()
or stop()
. Remember, use ?function
to access the Help page. Add additional logic to the function, as needed.
When you run find_max(4, 2, 5)
, the following error message appears:
Error in find_max(4, 2, 5) : unused argument (5)
This is an error message that is automatically created by R since our function only requires two parameters. This is a sufficient error message.
Run find_max(4, 4)
in the Console.
What happens? What kind of message appears? Is it sufficient? If not, consider adding a warning or error message using warning()
or stop()
. Remember, use ?function
to access the Help page. Add additional logic to the function, as needed.
When you run find_max(4, 4)
, no message appears and the function is sent to the Console, but no value is either returned or printed.
To account for this scenario, add an if()
statement to the beginning of the function, and then use either warning()
or stop()
.
# `find_max()` function with error message
<- function(value_1, value_2) {
find_max
if (value_1 == value_2) {
stop("Values must be different from each other.")
}
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
}
}
# try it out #
# does the message appear as you expected?
find_max(4, 4)
Run find_max(4, "cow")
in the Console.
What happens? What kind of message appears? Is it sufficient? If not, consider adding a warning or error message using warning()
or stop()
. Remember, use ?function
to access the Help page. Add additional logic to the function, as needed.
When you run find_max(4, "cow")
, the function runs as is and returns the value “cow”. This is not expected because these two values aren’t necessarily comparable since they’re different data types.
However, find_max()
doesn’t know any better since this scenario hasn’t been defined in the body of the function yet.
To account for this scenario, add additional logic that checks the class of each argument before the function continues to execute.
The logical operator for OR is |
. The not-equal-to operator is !=
.
# `find_max()` function with error messages and checks
<- function(value_1, value_2) {
find_max
# `|` is the logical OR operator
# `!=` is the not-equal-to operator
if (is.numeric(value_1) != TRUE | is.numeric(value_2) != TRUE) {
# alt expression: is.numeric(value_1) == FALSE | is.numeric(value_2) == FALSE
stop("Value must be a numeric type.")
}
if (value_1 == value_2) {
stop("Values must be different from each other.")
}
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
}
}
# try it out #
# does the message appear as you expected?
find_max(4, "cow")
find_max("cow", 4)
aboutMe
PackageCreate the following two functions for the aboutMe
package:
format(Sys.Date(), "%Y")
.Sys.Date()
returns the current date, and format()
and "%Y"
formats the date to just the year in the format YYYY
(as opposed to YY
).Write each of these functions in their own scripts and save them to the R
directory.
Note: If any of your functions have dependencies, make sure to add it to the DESCRIPTION
file.
Go back to each function script and add a Roxygen skeleton to document each function.
Use usethis::use_testthat()
and usethis::use_test("test-file-name")
to test the age function.
aboutMe
Use either the Build Tab or run the devtools
functions test()
, check()
, and install()
in the Console.
After installing, go to the Packages Tab in the Files Pane, search for the aboutMe
package, and check that the documentation for your package looks as you expected. Try loading the package using library()
and call some of the functions you created.