diff --git a/materials/sections/git-github-intro.qmd b/materials/sections/git-github-intro.qmd index 6fe658f8..4081e787 100644 --- a/materials/sections/git-github-intro.qmd +++ b/materials/sections/git-github-intro.qmd @@ -65,7 +65,7 @@ Imagine that some time has gone by and you've committed a third version of your Maybe you're not so sure the idea will work out and this is where a tool like Git shines. Without a tool like Git, we might copy analysis.R to another file called analysis-ml.R which might end up having mostly the same code except for a few lines. This isn't particularly problematic until you want to make a change to a bit of shared code and now you have to make changes in two files, if you even remember to. -Instead, with Git, we can start a branch. Branches allow us to confidently experiment on our code, all while leaving the old code in tact and recoverable. +Instead, with Git, we can start a branch. Branches allow us to confidently experiment on our code, all while leaving the old code intact and recoverable. :::{.column-body-outset} ![](images/git-intro-slide06.png) diff --git a/materials/sections/r-practice-functions-and-packages.qmd b/materials/sections/r-practice-functions-and-packages.qmd index ecb70c40..292cdaa0 100644 --- a/materials/sections/r-practice-functions-and-packages.qmd +++ b/materials/sections/r-practice-functions-and-packages.qmd @@ -53,7 +53,7 @@ Create a function called `double_it()` that doubles any value input value. Then # create function # double_it <- function(x) { - print(2 * x) + return(2 * x) } # try it out # @@ -79,7 +79,7 @@ Then try out the function, are the values returned what you expect? # write function # exclaim_age <- function(age) { - print(paste("I am", age, "years old!")) + return(paste("I am", age, "years old!")) } # try it out # @@ -252,59 +252,58 @@ find_max(4, "cow") find_max("cow", 4) ``` - - - - - - - - - - - - - - - - - - - - - - - - - +::: {.callout-note} +#### Question 4d +Run `find_max(4, 4)` in the Console. Previously we coded our function to report an error. But perhaps the user would prefer to have the function return the shared value, as an option. Add an argument with a reasonable default value to allow the user to control this behavior. Add additional logic to the function, as needed. +::: - - +
+Hint 4d - +Before, if `value_1 == value_2`, we used `stop()` to create an error. But with an additional argument, we can adjust how the function responds by testing the value of that argument. - - +Often for arguments that turn on or turn off a behavior, a TRUE/FALSE value makes sense so you could easily include the argument in a logical test. - - +
- - - - - +```{r} +#| eval: false +#| code-fold: true +#| code-summary: Answer 4d Code - +# `find_max()` function with error messages and checks +find_max <- function(value_1, value_2, equal_ok = FALSE) { + + # `|` 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) { + ### the values are equal; is value of the equal_ok argument TRUE? + if(equal_ok) return(value_1) + ### if equal_ok is not TRUE, then report an error + 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) +find_max(4, 4, equal_ok = TRUE) +``` - ::: {.callout-caution icon=false} diff --git a/materials/session_07.qmd b/materials/session_07.qmd index 0d7adc77..73181427 100644 --- a/materials/session_07.qmd +++ b/materials/session_07.qmd @@ -1,5 +1,5 @@ --- -title: "Git and GtiHub" +title: "Git and GitHub" title-block-banner: true --- diff --git a/materials/session_18.qmd b/materials/session_18.qmd index 59476546..632b4d2a 100644 --- a/materials/session_18.qmd +++ b/materials/session_18.qmd @@ -5,4 +5,4 @@ title-block-banner: true -{{< include /sections/r-practice-functions-and-packages.qmd >}} \ No newline at end of file +{{< include /sections/r-practice-functions-and-packages.qmd >}} \ No newline at end of file