Skip to content

Commit

Permalink
fixed typos in github lesson; added an extra example for function pra…
Browse files Browse the repository at this point in the history
…ctice (additional arguments with a default)
  • Loading branch information
oharac committed Oct 3, 2024
1 parent a9379c8 commit d47c348
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 deletions.
2 changes: 1 addition & 1 deletion materials/sections/git-github-intro.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
89 changes: 44 additions & 45 deletions materials/sections/r-practice-functions-and-packages.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand All @@ -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 #
Expand Down Expand Up @@ -252,59 +252,58 @@ find_max(4, "cow")
find_max("cow", 4)
```

<!-- ## Exercise: R Packages -->

<!-- ::: {.callout-tip icon=false} -->
<!-- ### Setup -->

<!-- 1. Create a new project using the R Package template by selecting: ```File -> New Project -> New Directory -> R Package -> Create Project```. In the R Package dialog box, type "aboutMe" in the "Package Name:" field. Or you can use `usethis::create_package("~/aboutMe")`. -->

<!-- 2. Delete the files: `hello.R` in the `R` directory and `hello.Rd` in the `man` directory. -->

<!-- 3. Add a license by running in the Console: ```usethis::use_apache_license()```. -->

<!-- 4. Turn the project into a Git repository. Recall the steps from [Ch 6 Exercise 3: Setting up Git on an existing project](https://learning.nceas.ucsb.edu/2023-10-coreR/session_06.html#exercise-3-setting-up-git-on-an-existing-project). Follow the steps either using the `usethis` package or Command Line. -->
<!-- ::: -->

<!-- ### Add Functions to the `aboutMe` Package -->

<!-- ::: {.callout-note} -->
<!-- #### Question 1 -->
<!-- Create the following two functions for the `aboutMe` package: -->

<!-- a. Function that prints out your name and your favorite color. -->
<!-- b. Function calculates your age for the current year. -->
<!-- i. **Hint**: You can access the current year using `format(Sys.Date(), "%Y")`. -->
<!-- ii. `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. -->
::: {.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.
:::

<!-- Note: If any of your functions have dependencies, make sure to add it to the `DESCRIPTION` file. -->
<!-- ::: -->
<details>
<summary>Hint 4d</summary>

<!-- ### Add Documentation -->
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.

<!-- ::: {.callout-note} -->
<!-- #### Question 2 -->
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.

<!-- Go back to each function script and add a Roxygen skeleton to document each function. -->
<!-- ::: -->
</details>

<!-- ### Test Age Function -->

<!-- ::: {.callout-note} -->
<!-- #### Question 3 -->
<!-- Use `usethis::use_testthat()` and `usethis::use_test("test-file-name")` to test the age function. -->
<!-- ::: -->
```{r}
#| eval: false
#| code-fold: true
#| code-summary: Answer 4d Code
<!-- ### Test, Check, and Install `aboutMe` -->
# `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)
}
}
<!-- ::: {.callout-note} -->
<!-- #### Question 4 -->
<!-- Use either the Build Tab or run the `devtools` functions `test()`, `check()`, and `install()` in the Console. -->
<!-- ::: -->
# try it out #
# does the message appear as you expected?
find_max(4, 4)
find_max(4, 4, equal_ok = TRUE)
```

<!-- 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. -->


::: {.callout-caution icon=false}
Expand Down
2 changes: 1 addition & 1 deletion materials/session_07.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Git and GtiHub"
title: "Git and GitHub"
title-block-banner: true
---

Expand Down
2 changes: 1 addition & 1 deletion materials/session_18.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ title-block-banner: true



{{< include /sections/r-practice-functions-and-packages.qmd >}}
{{< include /sections/r-practice-functions-and-packages.qmd >}}

0 comments on commit d47c348

Please sign in to comment.