forked from swirldev/swirl_courses
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
30 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,10 @@ | ||
#' TODO: | ||
#' - Use testthat for more robust unit testing of functions? | ||
#' - Test that functions error appropriately | ||
#' - Create our own little wrapper around testthat or other DSL that | ||
#' simplifies unit testing functions for course authors? | ||
|
||
test_func1 <- function() { | ||
try({ | ||
func <- get('f', globalenv()) | ||
t1 <- identical(func(3), 9) | ||
t2 <- identical(func(-2), 4) | ||
func <- get('boring_function', globalenv()) | ||
t1 <- identical(func(9), 9) | ||
t2 <- identical(func(4), 4) | ||
t3 <- identical(func(0), 0) | ||
ok <- all(t1, t2, t3) | ||
}, silent = TRUE) | ||
exists('ok') && isTRUE(ok) | ||
} | ||
|
||
test_func2 <- function() { | ||
try({ | ||
func <- get('f', globalenv()) | ||
t1 <- identical(func(3, 3), 27) | ||
t2 <- identical(func(-2, 4), 16) | ||
t3 <- identical(func(0, 3), 0) | ||
t4 <- identical(func(-3, 0), 1) | ||
ok <- all(t1, t2, t3, t4) | ||
}, silent = TRUE) | ||
exists('ok') && isTRUE(ok) | ||
} | ||
|
||
test_func3 <- function() { | ||
try({ | ||
func <- get('min_positive', globalenv()) | ||
t1 <- identical(func(c(-4, 4, 2, 10)), 2) | ||
t2 <- identical(func(c(0, 9)), 0) | ||
t3 <- identical(func(c(-11, 0, 39)), 0) | ||
t4 <- identical(func(c(5, 6, 7, 7)), 5) | ||
ok <- all(t1, t2, t3, t4) | ||
}, silent = TRUE) | ||
exists('ok') && isTRUE(ok) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
- Class: meta | ||
Course: R Programming | ||
Lesson: Functions | ||
Author: Sean Kross | ||
Course: Test Course | ||
Lesson: Lesson to test script | ||
Author: Nick | ||
Type: Standard | ||
Organization: swirlstats | ||
Version: 2.2.14 | ||
Version: 2.2.12 | ||
|
||
- Class: text | ||
Output: Functions are one of the fundamental building blocks of the R language. They are small | ||
pieces of reusable code that can be treated like any other R object. | ||
- Class: script | ||
Output: Update the function f so that it accepts one argument, x, and returns the value of x squared. | ||
AnswerTests: test_func1() | ||
Hint: Make sure f accepts one argument called x and returns x^2. | ||
Script: test-script1.R | ||
|
||
- Class: text | ||
Output: Functions usually take arguments which are variables that the function operates on. For example, | ||
the mean() function takes a vector as as argument, like in the case of mean(c(2,6,8)) . The mean() | ||
function then adds up all of the numbers in the vector and divides that sum by the | ||
length of the vector. | ||
- Class: script | ||
Output: Now, have f accept a second argument, y, and return the value of x to the power of y. | ||
AnswerTests: test_func2() | ||
Hint: f should now accept two arguments, x and y, and return x^y. | ||
Script: test-script2.R | ||
|
||
- Class: script | ||
Output: The last R expression to be evaluated in a function is the return value of that function. | ||
We want this function to take one argument, x, and return x without modifying it. Delete the pound | ||
sign so that x is returned without any modification. | ||
AnswerTests: test_func1() | ||
Hint: Make sure to delete the pound sign so the last expression in the function is just x. | ||
Script: boring_function.R | ||
Output: A friend of yours is trying to write a function called min_positive that accepts a vector of numbers and returns the minimum value that is greater than or equal to zero. She keeps getting an error and can't figure out why. Be a good friend and fix min_positive, so that it does what she wants it to do. | ||
AnswerTests: test_func3() | ||
Hint: You need to fix the error and have the function return the appropriate value. | ||
Script: test-script3.R | ||
|
||
- Class: text | ||
Output: Now you know how functions work! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
boring_function <- function(x) { | ||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
boring_function <- function(x) { | ||
#x | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.