Skip to content

Commit

Permalink
new functions lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
seankross committed Aug 1, 2014
1 parent 068e042 commit 1b5d33c
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 83 deletions.
36 changes: 3 additions & 33 deletions R_Programming/Functions/customTests.R
@@ -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)
}
39 changes: 21 additions & 18 deletions R_Programming/Functions/lesson.yaml
@@ -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!
3 changes: 3 additions & 0 deletions R_Programming/Functions/scripts/boring_function-correct.R
@@ -0,0 +1,3 @@
boring_function <- function(x) {
x
}
3 changes: 3 additions & 0 deletions R_Programming/Functions/scripts/boring_function.R
@@ -0,0 +1,3 @@
boring_function <- function(x) {
#x
}
3 changes: 0 additions & 3 deletions R_Programming/Functions/scripts/test-script1-correct.R

This file was deleted.

3 changes: 0 additions & 3 deletions R_Programming/Functions/scripts/test-script1.R

This file was deleted.

3 changes: 0 additions & 3 deletions R_Programming/Functions/scripts/test-script2-correct.R

This file was deleted.

3 changes: 0 additions & 3 deletions R_Programming/Functions/scripts/test-script2.R

This file was deleted.

10 changes: 0 additions & 10 deletions R_Programming/Functions/scripts/test-script3-correct.R

This file was deleted.

10 changes: 0 additions & 10 deletions R_Programming/Functions/scripts/test-script3.R

This file was deleted.

0 comments on commit 1b5d33c

Please sign in to comment.