Skip to content

Commit

Permalink
Fleshed out the first half of lecture 2's slides
Browse files Browse the repository at this point in the history
  • Loading branch information
njlyon0 committed Jan 23, 2024
1 parent fbfca6d commit ac9469a
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 12 deletions.
4 changes: 2 additions & 2 deletions _freeze/materials/slides_2a/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "f02ab99023d3b7aabd383255b056aaf6",
"hash": "c65fdb2924eff1b8d6d2666d9bcde8db",
"result": {
"markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lecture 2 -- Packages, Structure, & Conditionals\"\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- R Packages\n- Class vs. Structure\n- Using Vectors\n- Conditionals\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Load and use an R package\n- Define the difference(s) between object <u>class</u> and <u>structure</u>\n- Create and manipulate vectors\n- Write conditional statements\n- Manage missing data in objects with conditionals\n\n## [R Package Background]{.orange}\n\n\n\n## [Package Locations]{.orange}\n\n\n## [Using Packages]{.orange}\n\n\n## [Package Analogy]{.gold}\n\n\n## [Practice: Packages]{.pink}\n\n\n## [Package Namespacing]{.orange}\n\n\n## [Practice: Namespacing]{.pink}\n\n\n## [Class versus Structure]{.orange}\n\n\n## [Checking Class/Structure]{.orange}\n\n\n## [Practice: Structure]{.pink}\n\n\n## [Using Vectors: Coordinates]{.orange}\n\n\n## [Using Vectors: Coordinates Cont.]{.orange}\n\n\n## [Practice: Vector Navigation]{.pink}\n\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## [Conditionals]{.orange}\n\n\n## [Conditionals: EQUAL, OR, & AND]{.orange}\n\n\n## [Practice: Fundamental Conditionals]{.pink}\n\n\n## [Discussion: Conditionals]{.gold}\n\n\n## [Numeric Conditionals]{.orange}\n\n\n## [Practice: Numeric Conditionals]{.orange}\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::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Due before lab\n\n#### (By midnight)\n\n- Muddiest Point \\#x\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Due before lecture\n\n#### (By midnight)\n\n- X\n\n:::\n\n::::\n\n# [Bonus Conditionals]{.purple}\n\n## [OR with >2 Options]{.orange}\n\n\n## [Conditionals: NOT]{.orange}\n\n",
"markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lecture 2 -- Packages, Structure, & Conditionals\"\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- R Packages\n- Class vs. Structure\n- Using Vectors\n- Conditionals\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Load and use an R package\n- Define the difference(s) between object <u>class</u> and <u>structure</u>\n- Create and manipulate vectors\n- Write conditional statements\n- Manage missing data in objects with conditionals\n\n## [R Package Background]{.orange} {.smaller}\n\n- R packages are suites of functions developed by users\n\n\\\n\n- Packages have no defined depth or breadth requirements\n - A package could be a single, simple function\n - Or a complex ecosystem of inter-related functions\n\n\\\n\n- Packages can be installed by any R user for _free!_\n\n\\\n\n- R is versatile and powerful (in part) because of contributed packages\n\n## [Package Locations]{.orange}\n\n- There are two main homes for R packages\n\n\\\n\n. . . \n\n1. Comprehensive R Archive Network\n - A.k.a. \"CRAN\"\n - [cran.rstudio.com](https://cran.rstudio.com/)\n\n2. GitHub\n - [github.com](https://github.com/)\n\n## [Package Locations Cont.]{.orange} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n#### CRAN\n\n- Currently >20,000 packages live here\n\n\\\n\n- Strict rules for packages to be allowed\n\n\\\n\n- These are \"official\" packages\n\n:::\n\n:::{.column width=\"50%\"}\n\n#### GitHub\n\n- Unknown number of packages here (no centralized record retained)\n\n\\\n\n- No mandatory quality control tests to be available here\n\n\\\n\n- Packages _usually_ work but they don't have the same quality control as CRAN packages\n\n:::\n\n::::\n\n## [Using Packages]{.orange}\n\n- In order to use a package, you must:\n\n\\\n\n1. <u>Install</u> the desired package\n - Done once per computer\n\n\\\n\n2. <u>Load</u> the package into R\n - Done _every_ time you re-open RStudio\n\n## [Using Packages: Specific Steps]{.orange}\n\n\\\n\n```{.r}\n# Install desired package\ninstall.packages(\"dplyr\")\n\n# Load that package\nlibrary(dplyr)\n```\n\n\\\n\n- `install.packages` <u>_requires_</u> the package name be in quotes\n- `library` <u>_requires_</u> requires **unquoted** names.\n\n## [Package Analogy]{.gold}\n\n. . . \n\n- `install.packages` = buying a set of tools from the store and putting them in your home\n\n\\\n\n. . .\n\n- `library` = moving the tools that you already own to your workbench\n\n\\\n\n. . .\n\n- You only _buy_ the tools **once** but _every time_ you start work you need to bring them back to your work area!\n\n## [Practice: Packages]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\"}\n\n- Make a new script for this week's lecture!\n - Save it in your RStudio Project folder for this course\n - Make sure it has \"lecture\" and \"2\" in the file name\n\n\\\n\n. . .\n\n- Install the `palmerpenguins` package\n - Remember to put quotes around the package name!\n\n\\\n\n. . .\n\n- Load the package with the `library` function\n - Once loaded, run `?palmerpenguins` to see the package-level help file\n\n## [Note on _Function_ Names]{.orange} {.smaller}\n\n- Functions <u>_are not_</u> required to have unique names across all packages\n\n\\\n\n- Risk of using a different function than intended\n - Best case: creates an error and forces you to catch the mistake\n - Worst case: _silently_ does something wrong\n\n\\\n\n- Functions can be \"namespaced\" to specify which package the function comes from\n\n## [Function Namespacing]{.orange}\n\n- Namespacing _guarantees_ you use the function from the desired package\n\n\\\n\n- Done with two colons (:) between the package and function names\n - E.g., `package::function()`\n\n## [Practice: Namespacing]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\"}\n![](images/hex-dplyr.png){.absolute top=0 left=965 width=\"12%\"}\n\n- `palmerpenguins` includes an example dataset on penguins\n\n. . .\n\n- Run the following code\n - `peng_df <- palmerpenguins::penguins`\n\n\\\n\n. . .\n\n- Install and load the `dplyr` package\n - `dplyr` has a function called `glimpse` that shows you core structures of data\n\n\\\n\n. . .\n\n- Namespace `glimpse` and run it on `peng_df`\n\n## [Class versus Structure]{.orange} {.smaller}\n\n- Objects have both [class]{.blue} and [structure]{.gold}\n\n\\\n\n. . .\n\n- [Class]{.blue} = the type of object it is\n - E.g., dataframe, integer, character, etc.\n\n\\\n\n. . .\n\n- [Structure]{.gold} = the dimensions and \"shape\" of the data\n - E.g., Number of rows / columns, length, etc.\n\n\\\n\n. . .\n\n- _Both_ class and structure affect what you can do with or to a given object!\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n. . .\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Check Class\n\n- Use the `class` function\n\n\\\n\n```{.r}\n# Checking class of 'my_obj'\nclass(my_obj)\n```\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n- Use _either_ the `str` function _or_ `dplyr::glimpse`\n\n```{.r}\n# Checking structure of 'my_obj' (with base R)\nstr(my_obj)\n\n# Checking structure of 'my_obj' (with `dplyr`)\ndplyr::glimpse(my_obj)\n```\n\n:::\n\n::::\n\n## [Practice: Structure]{.pink} {.smaller}\n\n- What is the [class]{.blue} of 'peng_df'?\n\n\\\n\n. . .\n\n- What is the [structure]{.gold}?\n - What information is included when you check?\n\n\\\n\n. . .\n\n- What happens when you check the [class]{.blue} of a function?\n - Run `class(class)`\n\n\\\n\n. . .\n\n- What happens when you check the [structure]{.gold} of a function?\n - Run `str(str)`\n\n## [Using Vectors: Coordinates]{.orange} {.smaller}\n\n- Vector structure is expressed as \"length\"\n - Vector length = number of elements in the vector\n - Dataframe length = number of rows\n\n\\\n\n- Bracket notation can be used to navigate vectors\n\n\\\n\n- Running `my_vec[5]` will return the fifth element of `my_vec`\n\n## [Using Vectors: Coordinates Cont.]{.orange} {.smaller}\n\n- Bracket notation accepts vectors of coordinates\n\n\\\n\n. . .\n\n- Running `my_vec[c(1, 4, 7)]` returns the first, fourth, and seventh elements of `my_vec`\n\n\\\n\n. . .\n\n- You can also grab the same element _more than once!_\n\n\\\n\n. . .\n\n- Running `my_vec[c(1, 1, 1)]` returns the first element three times \n\n## [Practice: Vector Navigation]{.pink} {.smaller}\n\n- Base R has a built-in vector of letters called `letters`\n - Assign `letters` to an object called `my_vec`\n\n\\\n\n- Check the length of `my_vec` using the `length` function\n\n\\\n\n. . .\n \n- Identify the 10th element of `my_vec` using bracket notation\n\n\\\n\n- Identify the 8^th^, 5^th^, 12^th^, 12^th^ (again), and 15^th^ elements of `my_vec`\n - Use concatenation inside of the brackets (with `c`)!\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## [Conditionals]{.orange}\n\n\n## [Conditionals: EQUAL, OR, & AND]{.orange}\n\n\n## [Practice: Fundamental Conditionals]{.pink}\n\n\n## [Discussion: Conditionals]{.gold}\n\n\n## [Numeric Conditionals]{.orange}\n\n\n## [Practice: Numeric Conditionals]{.orange}\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::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Due before lab\n\n#### (By midnight)\n\n- Muddiest Point \\#x\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Due before lecture\n\n#### (By midnight)\n\n- X\n\n:::\n\n::::\n\n# [Bonus Conditionals]{.purple}\n\n## [OR with >2 Options]{.orange}\n\n\n## [Conditionals: NOT]{.orange}\n\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
Binary file added materials/images/hex-dplyr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added materials/images/hex-ggplot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added materials/images/hex-penguins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added materials/images/hex-tidyr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
316 changes: 306 additions & 10 deletions materials/slides_2a.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,341 @@ After today's session you will be able to:
- Write conditional statements
- Manage missing data in objects with conditionals

## [R Package Background]{.orange}
## [R Package Background]{.orange} {.smaller}

- R packages are suites of functions developed by users

\

- Packages have no defined depth or breadth requirements
- A package could be a single, simple function
- Or a complex ecosystem of inter-related functions

\

- Packages can be installed by any R user for _free!_

\

- R is versatile and powerful (in part) because of contributed packages

## [Package Locations]{.orange}

- There are two main homes for R packages

\

. . .

1. Comprehensive R Archive Network
- A.k.a. "CRAN"
- [cran.rstudio.com](https://cran.rstudio.com/)

2. GitHub
- [github.com](https://github.com/)

## [Package Locations Cont.]{.orange} {.smaller}

::::{.columns}

:::{.column width="50%"}

#### CRAN

- Currently >20,000 packages live here

\

- Strict rules for packages to be allowed

\

- These are "official" packages

:::

:::{.column width="50%"}

#### GitHub

- Unknown number of packages here (no centralized record retained)

\

- No mandatory quality control tests to be available here

\

- Packages _usually_ work but they don't have the same quality control as CRAN packages

:::

::::

## [Using Packages]{.orange}

- In order to use a package, you must:

\

1. <u>Install</u> the desired package
- Done once per computer

\

2. <u>Load</u> the package into R
- Done _every_ time you re-open RStudio

## [Using Packages: Specific Steps]{.orange}

\

```{.r}
# Install desired package
install.packages("dplyr")
# Load that package
library(dplyr)
```

\

- `install.packages` <u>_requires_</u> the package name be in quotes
- `library` <u>_requires_</u> requires **unquoted** names.

## [Package Analogy]{.gold}

. . .

- `install.packages` = buying a set of tools from the store and putting them in your home

\

. . .

- `library` = moving the tools that you already own to your workbench

\

. . .

- You only _buy_ the tools **once** but _every time_ you start work you need to bring them back to your work area!

## [Practice: Packages]{.pink} {.smaller}

![](images/hex-penguins.png){.absolute top=0 left=1100 width="12%"}

- Make a new script for this week's lecture!
- Save it in your RStudio Project folder for this course
- Make sure it has "lecture" and "2" in the file name

\

## [Practice: Packages]{.pink}
. . .

- Install the `palmerpenguins` package
- Remember to put quotes around the package name!

\

. . .

- Load the package with the `library` function
- Once loaded, run `?palmerpenguins` to see the package-level help file

## [Note on _Function_ Names]{.orange} {.smaller}

- Functions <u>_are not_</u> required to have unique names across all packages

\

- Risk of using a different function than intended
- Best case: creates an error and forces you to catch the mistake
- Worst case: _silently_ does something wrong

\

## [Package Namespacing]{.orange}
- Functions can be "namespaced" to specify which package the function comes from

## [Function Namespacing]{.orange}

## [Practice: Namespacing]{.pink}
- Namespacing _guarantees_ you use the function from the desired package

\

## [Class versus Structure]{.orange}
- Done with two colons (:) between the package and function names
- E.g., `package::function()`

## [Practice: Namespacing]{.pink} {.smaller}

## [Checking Class/Structure]{.orange}
![](images/hex-penguins.png){.absolute top=0 left=1100 width="12%"}
![](images/hex-dplyr.png){.absolute top=0 left=965 width="12%"}

- `palmerpenguins` includes an example dataset on penguins

## [Practice: Structure]{.pink}
. . .

- Run the following code
- `peng_df <- palmerpenguins::penguins`

\

. . .

- Install and load the `dplyr` package
- `dplyr` has a function called `glimpse` that shows you core structures of data

\

. . .

- Namespace `glimpse` and run it on `peng_df`

## [Using Vectors: Coordinates]{.orange}
## [Class versus Structure]{.orange} {.smaller}

- Objects have both [class]{.blue} and [structure]{.gold}

## [Using Vectors: Coordinates Cont.]{.orange}
\

. . .

- [Class]{.blue} = the type of object it is
- E.g., dataframe, integer, character, etc.

\

. . .

- [Structure]{.gold} = the dimensions and "shape" of the data
- E.g., Number of rows / columns, length, etc.

\

. . .

- _Both_ class and structure affect what you can do with or to a given object!

## [Checking Class/Structure]{.orange} {.smaller}

- Best to **check class and structure** of an object to ensure functions will work

. . .

::::{.columns}

:::{.column width="50%"}

### Check Class

- Use the `class` function

\

```{.r}
# Checking class of 'my_obj'
class(my_obj)
```

:::

:::{.column width="50%"}

### Check Structure

- Use _either_ the `str` function _or_ `dplyr::glimpse`

```{.r}
# Checking structure of 'my_obj' (with base R)
str(my_obj)
# Checking structure of 'my_obj' (with `dplyr`)
dplyr::glimpse(my_obj)
```

:::

::::

## [Practice: Structure]{.pink} {.smaller}

- What is the [class]{.blue} of 'peng_df'?

\

. . .

- What is the [structure]{.gold}?
- What information is included when you check?

\

. . .

- What happens when you check the [class]{.blue} of a function?
- Run `class(class)`

\

. . .

- What happens when you check the [structure]{.gold} of a function?
- Run `str(str)`

## [Using Vectors: Coordinates]{.orange} {.smaller}

- Vector structure is expressed as "length"
- Vector length = number of elements in the vector
- Dataframe length = number of rows

\

- Bracket notation can be used to navigate vectors

\

- Running `my_vec[5]` will return the fifth element of `my_vec`

## [Using Vectors: Coordinates Cont.]{.orange} {.smaller}

- Bracket notation accepts vectors of coordinates

\

. . .

- Running `my_vec[c(1, 4, 7)]` returns the first, fourth, and seventh elements of `my_vec`

\

. . .

- You can also grab the same element _more than once!_

\

. . .

- Running `my_vec[c(1, 1, 1)]` returns the first element three times

## [Practice: Vector Navigation]{.pink} {.smaller}

- Base R has a built-in vector of letters called `letters`
- Assign `letters` to an object called `my_vec`

\

- Check the length of `my_vec` using the `length` function

\

. . .

- Identify the 10th element of `my_vec` using bracket notation

## [Practice: Vector Navigation]{.pink}
\

- Identify the 8^th^, 5^th^, 12^th^, 12^th^ (again), and 15^th^ elements of `my_vec`
- Use concatenation inside of the brackets (with `c`)!

## [Temperature Check]{.purple}

Expand Down

0 comments on commit ac9469a

Please sign in to comment.