Skip to content

Commit

Permalink
Added some of my 'wrap up' slides from 8B to 8A (due to scheduling, n…
Browse files Browse the repository at this point in the history
…ot doing 8B this year)
  • Loading branch information
njlyon0 committed Jul 16, 2024
1 parent a737ae3 commit 3515c4e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions _freeze/materials/slides_8a/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"hash": "fdb4f0f74cdcc590cde0d406431964af",
"hash": "ec0794e20d6db80f46198eb743b36bd2",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lecture 8 -- Custom Functions\"\nengine: knitr\nformat: \n revealjs: \n slide-number: c\n scrollable: false\n code-overflow: wrap\n code-line-numbers: false\n code-copy: hover\n theme: [night, slides.scss]\n reference-location: document\n footer: \"[Programming in R for Biologists](https://njlyon0.github.io/teach_r-for-biologists/)\"\n---\n\n\n## A Guide to Your Process\n\n### [Scheduling]{.blue}\n\n### [Learning Objectives]{.purple}\n\n### [Practice]{.pink}\n\n### [Supporting Information]{.orange}\n\n### [Class Discussion]{.gold}\n\n## [Today's Plan]{.blue}\n\n- Muddiest Point Review\n- Custom Functions\n- Free Work\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Describe the process of writing a new function\n- Create a custom function to perform arithmetic\n\n## [Muddiest Point Review]{.gold}\n\n- Recurring topics from most recent MPs:\n\n\\\n\n. . . \n\n- What other topic(s) would you like to review?\n\n## [What is a \"Custom Function\"?]{.orange} {.smaller}\n\n- We've been working with functions throughout the course\n - These functions have come from packages we can install from CRAN\n\n\\\n\n. . .\n\n- However, R _also_ lets you write your own functions!\n\n\\\n\n. . .\n\n- Functions _you_ write are called [custom functions]{.purple}\n\n## [Why Write Functions?]{.orange} {.smaller}\n\n- Rewriting / duplicating anything is _risky_\n - Chance for human error when you're typing\n - Typos make each \"copy\" potentially give different results\n\n\\\n\n. . .\n\n- Instead you could write/use a custom function!\n\n\\\n\n. . .\n\n- Functions are a \"single source of truth\"\n - Something went wrong? Fix the function!\n\n\\\n\n. . .\n\n- Can also share function scripts with friends to make their lives easier\n\n## [Special Syntax Note]{.orange} {.smaller}\n\n- R Uses the following special syntax for creating functions\n - You have to use the `function` function\n\n\\\n\n. . . \n\n- Example syntax:\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"|2|5|8|10|13\"}\n# Define function name\nmultiply <- function(arg1, arg2){\n \n # Multiply the arguments together\n result <- arg1 * arg2\n \n # Return that to the user\n return(result)\n \n} # End function operations\n\n# Once created, it can be used!\nmultiply(arg1 = 10, arg2 = 5)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 50\n```\n\n\n:::\n:::\n\n\n. . . \n\n- Looks kind of like a conditional or loop, right?\n\n## [Process of Function Writing]{.purple} {.smaller}\n\n- My tips for success in custom function writing:\n\n. . .\n\n1. Write a normal script that does what you want your function to do\n\n\\\n\n. . .\n\n2. Identify which part(s) of the script you want the function user to specify\n - These will be the arguments!\n\n\\\n\n. . .\n\n3. Do the special formatting to make R recognize it as a function\n\n\\\n\n. . .\n\n4. Use the function like you would any other!\n\n## [Custom Function Demo]{.gold} {.smaller}\n\n- Let's create a function together for practice!\n\n\\\n\n. . .\n\n- Following my tips:\n 1. Write the script version of the function\n 2. Figure out what the function user should have control over\n 3. Make R consider it a function\n 4. Use our new function!\n\n## [Demo Step 1: Write Script]{.gold} {.smaller}\n\n- Let's make a function that adds two numbers together\n - Artificially simple, I know, but useful to learn with!\n\n\\\n\n. . .\n\n- We begin by making the script version of our function\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"true\"}\n# Add two numbers together\n4 + 3\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n## [Demo Step 2: Identify Inputs]{.gold} {.smaller}\n\n- Now we identify what the user of our function should be able to control\n\n\\\n\n. . .\n\n- If the function adds two numbers together, the user:\n - Should control the [first number]{.orange}\n - Should control the [second number]{.orange}\n - Should <u>not</u> control the plus sign\n\n\\\n\n. . .\n\n- Let's replace our numbers with objects\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"true\"}\n# Make objects\nx <- 4\ny <- 3\n\n# Add two objects together\nx + y\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n## [Demo Step 3: Special Format]{.gold} {.smaller}\n\n- We can now make this a real function!\n\n\\\n\n. . .\n\n- Define the function using special R syntax\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"|2|5|8|9\"}\n# Define function\nplus <- function(x, y){\n \n # Add arguments together\n result <- x + y\n \n # Return that\n return(result)\n}\n```\n:::\n\n\n## [Demo Step 4: Use Function!]{.gold} {.smaller}\n\n- Now that the function exists, we can use it just like any other function\n\n\\\n\n. . .\n\n- Add 4 and 3 together using our new function\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"true\"}\n# Define function\nplus(x = 4, y = 3)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n## [Write Your Own!]{.pink} {.smaller}\n\n- Write a function that converts pounds (lb) to kilograms (kg)\n - Formula: (pound / 2.2) = kilograms\n\n\\\n\n- Remember my tips:\n 1. Write it like a normal script\n 2. Replace values a user would change with objects\n 3. Use special R syntax\n 4. Then use the function!\n\n\\\n\n- Let me know when you've finished your function\n - Or if you need help!\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n<p align=\"center\">\n<img src=\"comics/debugging.png\" alt=\"Comic-style graph depicting someone's emotional state as they debug code (from initial struggle and defeat to eventual triumph)\">\n</p>\n\n## [Argument Defaults]{.orange} {.smaller}\n\n- You can set argument defaults when you create a function\n\n\\\n\n. . .\n\n- Advantage: user doesn't have to specify all arguments\n\n\\\n\n. . .\n\n- How do we do this?\n - Set it in the `function` _parentheses_!\n\n## [Default Example]{.orange} {.smaller}\n\n- Let's define defaults in our addition function\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"|2|11\"}\n# Define function\nplus <- function(x = 4, y = 3){\n \n # Add arguments together\n result <- x + y\n \n # Return that\n return(result) }\n```\n:::\n\n\n\\\n\n. . .\n\n- We can then use the function without specifying those arguments\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"false\"}\nplus()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- If the user specifies an argument differently, their input \"wins\"\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"false\"}\nplus(x = 10)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 13\n```\n\n\n:::\n:::\n\n\n## [Make a Default]{.pink} {.smaller}\n\n- Let's revisit your pound to kilogram function\n\n\\\n\n. . .\n\n- Set the default pounds to **175**\n\n\\\n\n. . .\n\n- What happens if you run your function with _nothing_ in the parentheses?\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n<p align=\"center\">\n<img src=\"comics/debugging.png\" alt=\"Comic-style graph depicting someone's emotional state as they debug code (from initial struggle and defeat to eventual triumph)\">\n</p>\n\n## [Upcoming Due Dates]{.blue} {.smaller}\n\n### Due before 7/22\n\n#### (By midnight)\n\n- Final deadline for **_all_** work from a summer course\n - Homework \\#8\n - GitHub Presence evaluation\n - _Optional_ [3^rd^ draft]{.pink} of Function Tutorials\n\n## [Free Work]{.gold} {.smaller}\n\n\n- Everything is due at the end of course (_soon!_)\n\n\\\n\nTips for success:\n\n. . .\n\n1. Check out rubrics and make sure you don't miss any \"easy\" points\n\n. . .\n\n2. Don't leave after this slide!\n\n. . .\n\n3. If you have questions, **ask them _now_** during this free work time\n",
"markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lecture 8 -- Custom Functions\"\nengine: knitr\nformat: \n revealjs: \n slide-number: c\n scrollable: false\n code-overflow: wrap\n code-line-numbers: false\n code-copy: hover\n theme: [night, slides.scss]\n reference-location: document\n footer: \"[Programming in R for Biologists](https://njlyon0.github.io/teach_r-for-biologists/)\"\n---\n\n\n## A Guide to Your Process\n\n### [Scheduling]{.blue}\n\n### [Learning Objectives]{.purple}\n\n### [Practice]{.pink}\n\n### [Supporting Information]{.orange}\n\n### [Class Discussion]{.gold}\n\n## [Today's Plan]{.blue}\n\n- Muddiest Point Review\n- Custom Functions\n- Free Work\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Describe the process of writing a new function\n- Create a custom function to perform arithmetic\n\n## [Muddiest Point Review]{.gold}\n\n- Recurring topics from most recent MPs:\n\n\\\n\n. . . \n\n- What other topic(s) would you like to review?\n\n## [What is a \"Custom Function\"?]{.orange} {.smaller}\n\n- We've been working with functions throughout the course\n - These functions have come from packages we can install from CRAN\n\n\\\n\n. . .\n\n- However, R _also_ lets you write your own functions!\n\n\\\n\n. . .\n\n- Functions _you_ write are called [custom functions]{.purple}\n\n## [Why Write Functions?]{.orange} {.smaller}\n\n- Rewriting / duplicating anything is _risky_\n - Chance for human error when you're typing\n - Typos make each \"copy\" potentially give different results\n\n\\\n\n. . .\n\n- Instead you could write/use a custom function!\n\n\\\n\n. . .\n\n- Functions are a \"single source of truth\"\n - Something went wrong? Fix the function!\n\n\\\n\n. . .\n\n- Can also share function scripts with friends to make their lives easier\n\n## [Special Syntax Note]{.orange} {.smaller}\n\n- R Uses the following special syntax for creating functions\n - You have to use the `function` function\n\n\\\n\n. . . \n\n- Example syntax:\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"|2|5|8|10|13\"}\n# Define function name\nmultiply <- function(arg1, arg2){\n \n # Multiply the arguments together\n result <- arg1 * arg2\n \n # Return that to the user\n return(result)\n \n} # End function operations\n\n# Once created, it can be used!\nmultiply(arg1 = 10, arg2 = 5)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 50\n```\n\n\n:::\n:::\n\n\n. . . \n\n- Looks kind of like a conditional or loop, right?\n\n## [Process of Function Writing]{.purple} {.smaller}\n\n- My tips for success in custom function writing:\n\n. . .\n\n1. Write a normal script that does what you want your function to do\n\n\\\n\n. . .\n\n2. Identify which part(s) of the script you want the function user to specify\n - These will be the arguments!\n\n\\\n\n. . .\n\n3. Do the special formatting to make R recognize it as a function\n\n\\\n\n. . .\n\n4. Use the function like you would any other!\n\n## [Custom Function Demo]{.gold} {.smaller}\n\n- Let's create a function together for practice!\n\n\\\n\n. . .\n\n- Following my tips:\n 1. Write the script version of the function\n 2. Figure out what the function user should have control over\n 3. Make R consider it a function\n 4. Use our new function!\n\n## [Demo Step 1: Write Script]{.gold} {.smaller}\n\n- Let's make a function that adds two numbers together\n - Artificially simple, I know, but useful to learn with!\n\n\\\n\n. . .\n\n- We begin by making the script version of our function\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"true\"}\n# Add two numbers together\n4 + 3\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n## [Demo Step 2: Identify Inputs]{.gold} {.smaller}\n\n- Now we identify what the user of our function should be able to control\n\n\\\n\n. . .\n\n- If the function adds two numbers together, the user:\n - Should control the [first number]{.orange}\n - Should control the [second number]{.orange}\n - Should <u>not</u> control the plus sign\n\n\\\n\n. . .\n\n- Let's replace our numbers with objects\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"true\"}\n# Make objects\nx <- 4\ny <- 3\n\n# Add two objects together\nx + y\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n## [Demo Step 3: Special Format]{.gold} {.smaller}\n\n- We can now make this a real function!\n\n\\\n\n. . .\n\n- Define the function using special R syntax\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"|2|5|8|9\"}\n# Define function\nplus <- function(x, y){\n \n # Add arguments together\n result <- x + y\n \n # Return that\n return(result)\n}\n```\n:::\n\n\n## [Demo Step 4: Use Function!]{.gold} {.smaller}\n\n- Now that the function exists, we can use it just like any other function\n\n\\\n\n. . .\n\n- Add 4 and 3 together using our new function\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"true\"}\n# Define function\nplus(x = 4, y = 3)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n## [Write Your Own!]{.pink} {.smaller}\n\n- Write a function that converts pounds (lb) to kilograms (kg)\n - Formula: (pound / 2.2) = kilograms\n\n\\\n\n- Remember my tips:\n 1. Write it like a normal script\n 2. Replace values a user would change with objects\n 3. Use special R syntax\n 4. Then use the function!\n\n\\\n\n- Let me know when you've finished your function\n - Or if you need help!\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n<p align=\"center\">\n<img src=\"comics/debugging.png\" alt=\"Comic-style graph depicting someone's emotional state as they debug code (from initial struggle and defeat to eventual triumph)\">\n</p>\n\n## [Argument Defaults]{.orange} {.smaller}\n\n- You can set argument defaults when you create a function\n\n\\\n\n. . .\n\n- Advantage: user doesn't have to specify all arguments\n\n\\\n\n. . .\n\n- How do we do this?\n - Set it in the `function` _parentheses_!\n\n## [Default Example]{.orange} {.smaller}\n\n- Let's define defaults in our addition function\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"|2|11\"}\n# Define function\nplus <- function(x = 4, y = 3){\n \n # Add arguments together\n result <- x + y\n \n # Return that\n return(result) }\n```\n:::\n\n\n\\\n\n. . .\n\n- We can then use the function without specifying those arguments\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"false\"}\nplus()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 7\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- If the user specifies an argument differently, their input \"wins\"\n\n\n::: {.cell}\n\n```{.r .cell-code code-line-numbers=\"false\"}\nplus(x = 10)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 13\n```\n\n\n:::\n:::\n\n\n## [Make a Default]{.pink} {.smaller}\n\n- Let's revisit your pound to kilogram function\n\n\\\n\n. . .\n\n- Set the default pounds to **175**\n\n\\\n\n. . .\n\n- What happens if you run your function with _nothing_ in the parentheses?\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n<p align=\"center\">\n<img src=\"comics/debugging.png\" alt=\"Comic-style graph depicting someone's emotional state as they debug code (from initial struggle and defeat to eventual triumph)\">\n</p>\n\n## [My Goal for You]{.purple}\n\n#### From [Lecture 1]{.blue}!\n\n<p align=\"center\">\n<img src=\"comics/r-knowledge.png\" alt=\"Comic-style graph depicting someone's confidence with R changing over time\">\n</p>\n\n## [Your Accomplishments]{.gold} {.smaller}\n\n- In this class, [**you**]{.pink} have done the following:\n - Made and manipulated data\n - Created R scripts and RMarkdown reports\n - Explored version control & GitHub\n - Performed statistical tests\n - Written loops and your own custom function(s)\n - And so much more!\n\n\\\n\n. . .\n\n- If you have a CV/resume, definitely add:\n - Some (or all!) of these skills\n - Link to your GitHub profile\n\n## [Closing Thoughts]{.gold} {.smaller}\n\n- I had a lot of fun this summer with y'all!\n\n\\\n\n. . .\n\n- Your development as data scientists has been _amazing_ to witness\n\n\\\n\n. . .\n\n- I particularly loved how you all engaged with the Function Tutorials\n - You really made it your own\n - Found the intersection of your interests and R functions\n\n\\\n\n. . .\n\n- Do you have any last R / data things to discuss?\n\n## [Upcoming Due Dates]{.blue} {.smaller}\n\n### Due before 7/22\n\n#### (By midnight)\n\n- Final deadline for **_all_** work from a summer course\n - Homework \\#8\n - GitHub Presence evaluation\n - _Optional_ [3^rd^ draft]{.pink} of Function Tutorials\n\n## [Free Work]{.gold} {.smaller}\n\n\n- Everything is due at the end of course (_soon!_)\n\n\\\n\nTips for success:\n\n. . .\n\n1. Check out rubrics and make sure you don't miss any \"easy\" points\n\n. . .\n\n2. Don't leave after this slide!\n\n. . .\n\n3. If you have questions, **ask them _now_** during this free work time\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
50 changes: 50 additions & 0 deletions materials/slides_8a.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,56 @@ plus(x = 10)
<img src="comics/debugging.png" alt="Comic-style graph depicting someone's emotional state as they debug code (from initial struggle and defeat to eventual triumph)">
</p>

## [My Goal for You]{.purple}

#### From [Lecture 1]{.blue}!

<p align="center">
<img src="comics/r-knowledge.png" alt="Comic-style graph depicting someone's confidence with R changing over time">
</p>

## [Your Accomplishments]{.gold} {.smaller}

- In this class, [**you**]{.pink} have done the following:
- Made and manipulated data
- Created R scripts and RMarkdown reports
- Explored version control & GitHub
- Performed statistical tests
- Written loops and your own custom function(s)
- And so much more!

\

. . .

- If you have a CV/resume, definitely add:
- Some (or all!) of these skills
- Link to your GitHub profile

## [Closing Thoughts]{.gold} {.smaller}

- I had a lot of fun this summer with y'all!

\

. . .

- Your development as data scientists has been _amazing_ to witness

\

. . .

- I particularly loved how you all engaged with the Function Tutorials
- You really made it your own
- Found the intersection of your interests and R functions

\

. . .

- Do you have any last R / data things to discuss?

## [Upcoming Due Dates]{.blue} {.smaller}

### Due before 7/22
Expand Down

0 comments on commit 3515c4e

Please sign in to comment.