diff --git a/_freeze/materials/slides_1b/execute-results/html.json b/_freeze/materials/slides_1b/execute-results/html.json index c770f17..7855994 100644 --- a/_freeze/materials/slides_1b/execute-results/html.json +++ b/_freeze/materials/slides_1b/execute-results/html.json @@ -1,8 +1,8 @@ { - "hash": "3bf5b02b412e69e5fac30bdaf12e843e", + "hash": "d53a1289dd661555918f25a71696634b", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lab 1 -- RStudio & Base R\"\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- Installation Check (R + RStudio)\n- Navigating RStudio\n- RStudio Projects\n- R Scripts\n- \"Base\" R\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Set up RStudio on your computer\n- Create an RStudio Project\n- Use R Scripts\n- Define fundamental base R terms\n - E.g., object, assign, function, arguments, etc.\n- Solve simple arithmetic operations using R\n\n## [Installation Check: R & RStudio]{.gold}\n\n:::{.incremental}\n\n- Did anyone _not_ get [R]{.blue} installed?\n\n\\\n\n- Did anyone _not_ get [RStudio]{.orange} installed?\n\n\\\n\n- Once you have both installed, please open [RStudio]{.orange}\n\n:::\n\n## [R versus RStudio]{.orange} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n![](images/logo-r.png){.absolute top=100 left=150 width=\"15%\"}\n\\\n\\\n\\\n\\\n\n- Access to fundamental R operations\n- Not aesthetically pleasing\n - In my opinion ``{=html}\n- Faster for _very_ complex operations\n - Unlikely to matter for this course!\n\n:::\n\n:::{.column width=\"50%\"}\n\n![](images/logo-rstudio.png){.absolute top=100 left=580 width=\"35%\"}\n\\\n\\\n\\\n\\\n\n- Access to fundamental R operations\n- Allows custom aesthetics\n- Has quality-of-life improvements\n- Technically an \"[IDE]{.purple}\"\n - Integrated Developer Environment\n\n:::\n\n::::\n\n## [RStudio Layout]{.orange} {.smaller}\n\n- Layout consisting of four [panes]{.blue}\n - Each pane has [tabs]{.gold} that do different things\n - You can customize which tabs are in each pane!\n- To customize options:\n - In top menu, click \"Tools\"\n - Select \"Global Options\"\n - Select \"Pane Layout\"\n- I recommend the following layout:\n\n![](images/rstudio-layout.png){.absolute top=280 left=600 width=\"40%\"}\n\n## [Practice: Choose Your Colors]{.pink}\n\n- Tools ``{=html} Global Options ``{=html} Appearance \n\n- Look through some of the available color schemes\n\n- Feel free to share the name of your favorites!\n\n\\\n\n. . . \n\n- Once you've picked one that you're happy with:\n - Click \"OK\" (in bottom left)\n\n## [RStudio Projects]{.orange}\n\n:::{.incremental}\n\n- Projects are a special way of marking a folder on your computer for R\n\n\\\n\n- You can still use the folder for other stuff though!\n\n\\\n\n- RStudio will give you benefits if you use Projects\n\n:::\n\n## [Why Use Projects?]{.orange} {.smaller}\n\n- Sets default location for reading in / saving files\n - A.k.a. \"importing\" and \"exporting\" files\n\n\\\n\n. . .\n\n- Convenient separation among tasks you're working on!\n - RStudio 'remembers' which files are a part of each Project\n - Even re-opens whatever files you had opened last time you used the Project!\n\n\\\n\n. . .\n\n- Lowers risk of mixing up data / script files\n\n## [Practice: Create a Project]{.pink} {.smaller}\n\n1. Make an empty folder on your computer to make the Project in\n\n\\\n\n2. In RStudio, click \"Project: (None)\" in the top right corner\n - Choose \"New Project...\"\n - Pick \"Existing Directory\"\n - Click \"Browse...\" and find the folder you made in [step 1]{.pink}\n\n\\\n\n3. If the name of the folder you made in [step 1]{.pink} shows up in the top right corner of RStudio, it worked!\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n
\n\n
\n\n## [R Scripts]{.orange} {.smaller}\n\n- A code file is called a \"script\"\n - [Microsoft Word]{.blue}:[document]{.gold} -- [R]{.blue}:[script]{.gold}\n\n\\\n\n- Lines of code written in scripts can be re-used\n - I.e., re-\"run\"\n\n\\\n\n- Scripts can be run line-by-line manually or run all at once\n - Running all at once = \"sourcing\" a script\n\n## [Practice: Create an R Script]{.pink} {.smaller}\n\n- Create an R script\n - \"File\" ``{=html} \"New File\" ``{=html} \"R Script\"\n\n\\\n\n- Once created, save it as \"316_week1.R\"\n - \"File\" ``{=html} \"Save\"\n\n\\\n\n- Note that the save location defaults to inside of your RStudio Project\n - Another reason to use Projects!\n\n\n## [Base R Fundamentals]{.orange} {.smaller}\n\n- \"Base R\" includes a set of core functions that are broadly useful\n - Reading in a CSV, taking an average, etc.\n\n\\\n\n- R's real power comes from \"packages\" with more advanced functions\n - These packages are _not_ in base R\n - We'll begin to cover these next week\n\n\\\n\n- Base R is really useful in its own right!\n\n## [Objects and Assignment]{.orange} {.smaller}\n\n- You can do math in R directly\n\n\\\n\n- More useful to use [objects]{.blue} instead of direct values\n - [object]{.blue} = alias for a value / set of values\n\n\\\n\n- When we create an object we [assign]{.gold} some value(s) to the object\n - Uses the [assignment operator]{.orange} (`<-`)\n - Keyboard shortcut: Alt + hyphen\n\n\\\n\n- After you create an object, you can use it in place of its value!\n\n## [Object Example]{.gold}\n\nWe can make an object like so:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmy_obj <- 52\n```\n:::\n\n\n\\\n\n. . . \n\nThen when we use the name of the object later...\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmy_obj\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 52\n```\n\n\n:::\n:::\n\n\n\\\n\n... we get the value we assigned to it!\n\n## [Objects Continued]{.orange}\n\n- Useful because it lets you _flexibly_ code \n\n\\\n\n- If you know that an object only contains numbers, you can do general operations that work for any number object\n\n\\\n\n- If you had to use the actual value every time, complex operations would be brutal to code\n\n## [Practice: Object Assignment]{.pink} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n#### Arithmetic\n\n- Calculate 576 / 3\n\n\\\n\n- Calculate (258 * 4) / 13\n\n\\\n\n- Calculate 2 + 2 - 4\n\n:::\n\n:::{.column width=\"50%\"}\n\n#### Object Practice\n\n- Create an object called `yy` with a value of 55\n - _Hint_: remember to use the assignment operator (`<-`)\n\n\\\n\n- Create an object called `my_char` with a value of \"text\"\n - What happens when you don't include quotation marks around \"text\"?\n\n:::\n\n::::\n\n## [Code Comments]{.orange} {.smaller}\n\n- Coding languages all allow [comments]{.gold}\n\n\\\n\n- Comments are _non-coding_ lines that explain what the code is doing in a human-readable way\n\n\\\n\n- In R, comment lines start with a hashtag (`#`)\n\n\\\n\n- Comments allow others to read your code and understand what is happening\n - Great for _future you!!!_\n\n## [Code Comments]{.orange}\n\n\n\n
\n\n## [Code Example]{.gold}\n\nHere are some example comments:\n\n```{.r code-line-numbers=\"|1,4\"}\n# Make an object with a value of 77\nq <- 77\n\n# Divide it by 7 and assign to a new object\np <- q / 7\n```\n\n## [Practice: Comments]{.pink} {.smaller}\n\n- Add a comment at the top of your script with the course number and your name\n\n\\\n\n. . .\n\n- For each line of code you wrote earlier:\n - Add a comment line _above it_ explaining what that line does\n - Use wording that makes sense to you!\n\n\\\n\n. . .\n\n- This lets your class notes be included directly with your code!\n\n## [Functions]{.orange} {.smaller}\n\n- Programming wouldn't be useful if you could only use arithmetic\n\n\\\n\n- Programming languages include [functions]{.blue} to avoid arithmetic\n - Someone else has done the arithmetic and made an object **of that process**!\n\n\\\n\n- You can then use the function instead of doing the math yourself\n\n\\\n\n- Functions use [arguments]{.orange} to let users specify what they can modify\n\n## [Function Example]{.gold} {.smaller}\n\n- There is a function called `round` that rounds decimals either up or down\n\n\\\n\n- `round` has two arguments:\n - `x` = the number(s) to round\n - `digits` = the number of digits to which to round `x`\n\n\\\n\n\n- Let's look at an example\n\n. . . \n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Round a decimal to 2 digits\nround(x = 62.82379110, digits = 2)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 62.82\n```\n\n\n:::\n:::\n\n\n## [Function Help]{.gold}\n\n- Functions include help files built into R to help you use them!\n\n\\\n\n- Can be accessed by typing a `?` and then the function name\n - E.g., `?mean`\n\n\\\n\n. . . \n\nType and run `?round` and see what happens\n\n## [Practice: Functions]{.pink} {.smaller}\n\n- Base R includes an object called `pi` that includes the first 6 digits of π\n\n\\\n\n- Using the `round` function, round `pi` to 3 digits\n\n\\\n\n. . .\n\n- Consult the help file for the square root function (`sqrt`)\n - What argument(s) does `sqrt` expect?\n\n\\\n\n- What is the square root of pi?\n\n\\\n\n. . .\n\n- Don’t forget to add comments to each line in your script!\n\n## [Vectors and Concatenation]{.orange} {.smaller}\n\n- You can also make objects that contain several values\n - This type of object is called a [vector]{.purple}\n\n\\\n\n- You can make vectors using \"concatenation\"\n - \\[con-CAT-eh-nation\\]\n\n\\\n\n- R does this with a lower case `c` and parentheses\n - You need commas between each [element]{.orange} of a vector\n - E.g., `my_vector <- c(1, 2, 3, 4)`\n\n## [Object Classes]{.orange} {.smaller}\n\n- Objects have a [class]{.blue} that dictates what operations they support\n\n\\\n\n- Math can only be done on \"numeric\" objects\n\n\\\n\n- Text can be stored in \"character\" objects\n - But math can’t be done on characters!\n\n\\\n\n- You can check an object's current class with the `class` function\n - E.g., `class(my_object)`\n\n## [Object Classes Cont.]{.orange}\n\n- There are _many_ classes\n\n\\\n\n- Let's focus on 4 fundamental classes\n - Numeric = numbers\n - Integer = numbers without _any_ decimal places\n - Character = text\n - Factor = text but defines categorical groups\n\n## [Class Coercion]{.orange} {.smaller}\n\n- You can force (a.k.a. [coerce]{.pink}) an object from one class to another\n\n\\\n\n- The simplest functions to do this are all `as.\n\n
\n\n## [Upcoming Due Dates]{.blue} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Due before lecture\n\n#### (By midnight)\n\n- Homework \\#1\n- Read the British Ecological Society's [Reproducible Code Guide](https://www.britishecologicalsociety.org/wp-content/uploads/2017/12/guide-to-reproducible-code.pdf) (p.1-12)\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Due before lab\n\n#### (By midnight)\n\n- Muddiest Point \\#1\n- Read the British Ecological Society's [Reproducible Code Guide](https://www.britishecologicalsociety.org/wp-content/uploads/2017/12/guide-to-reproducible-code.pdf) (p.20-25)\n\n:::\n\n::::\n\n", + "markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lab 1 -- RStudio & Base R\"\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- Installation Check (R + RStudio)\n- Navigating RStudio\n- RStudio Projects\n- R Scripts\n- \"Base\" R\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Set up RStudio on your computer\n- Create an RStudio Project\n- Use R Scripts\n- Define fundamental base R terms\n - E.g., object, assign, function, arguments, etc.\n- Solve simple arithmetic operations using R\n\n## [Installation Check: R & RStudio]{.gold}\n\n:::{.incremental}\n\n- Did anyone _not_ get [R]{.orange} installed?\n\n\\\n\n- Did anyone _not_ get [RStudio]{.orange} installed?\n\n\\\n\n- Once you have both installed, please open [RStudio]{.orange}\n\n:::\n\n## [R versus RStudio]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n![](images/logo-r.png){.absolute top=100 left=150 width=\"15%\" fig-alt=\"R logo\"}\n\n:::\n:::{.column width=\"50%\"}\n\n![](images/logo-rstudio.png){.absolute top=100 left=580 width=\"35%\" fig-alt=\"RStudio logo\"}\n\n:::\n::::\n\n## [R versus RStudio]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n![](images/logo-r.png){.absolute top=100 left=150 width=\"15%\" fig-alt=\"R logo\"}\n\\\n\\\n\\\n\\\n\n- Access to fundamental R operations\n- Not aesthetically pleasing\n - In my opinion ``{=html}\n- Faster for _very_ complex operations\n - Unlikely to matter for this course!\n\n:::\n:::{.column width=\"50%\"}\n\n![](images/logo-rstudio.png){.absolute top=100 left=580 width=\"35%\" fig-alt=\"RStudio logo\"}\n\n:::\n::::\n\n## [R versus RStudio]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n![](images/logo-r.png){.absolute top=100 left=150 width=\"15%\" fig-alt=\"R logo\"}\n\\\n\\\n\\\n\\\n\n- Access to fundamental R operations\n- Not aesthetically pleasing\n - In my opinion ``{=html}\n- Faster for _very_ complex operations\n - Unlikely to matter for this course!\n\n:::\n:::{.column width=\"50%\"}\n\n![](images/logo-rstudio.png){.absolute top=100 left=580 width=\"35%\" fig-alt=\"RStudio logo\"}\n\\\n\\\n\\\n\\\n\n- Access to fundamental R operations\n- Allows custom aesthetics\n- Has quality-of-life improvements\n- Technically an \"[IDE]{.orange}\"\n - Integrated Developer Environment\n\n:::\n::::\n\n## [RStudio Layout]{.orange} {.smaller}\n\n- Layout consisting of four [panes]{.purple}\n - Each pane has [tabs]{.orange} that do different things\n - You can customize which tabs are in each pane!\n- To customize options:\n - In top menu, click \"Tools\"\n - Select \"Global Options\"\n - Select \"Pane Layout\"\n- I recommend the following layout:\n\n![](images/rstudio-layout.png){.absolute top=260 left=600 width=\"40%\" fig-alt=\"Image of four squares arranged in a 2x2 grid. The top left is titled 'source', the bottom left is titled 'console' and the two right squares are labeled 'your choice'\"}\n\n## [Practice: Choose Your Colors]{.pink}\n\n- Tools ``{=html} Global Options ``{=html} Appearance \n\n\\\n\n- Look through some of the available color schemes\n - Feel free to share the name of your favorites!\n\n\\\n\n. . . \n\n- Once you've picked one that you're happy with:\n - Click \"OK\" (in bottom left)\n\n## [RStudio Projects]{.orange}\n\n:::{.incremental}\n\n- [Projects]{.purple} are a special way of marking a folder on your computer for R\n\n\\\n\n- You can still use the folder for other stuff though!\n\n\\\n\n- RStudio will give you benefits if you use Projects\n\n:::\n\n## [Why Use Projects?]{.orange} {.smaller}\n\n- Sets default location for reading in / saving files\n - A.k.a. \"importing\" and \"exporting\" files\n\n\\\n\n. . .\n\n- Convenient separation among tasks you're working on!\n - RStudio 'remembers' which files are a part of each Project\n - Even re-opens whatever files you had opened last time you used the Project!\n\n\\\n\n. . .\n\n- Lowers risk of mixing up data / script files\n\n## [Practice: Create a Project]{.pink} {.smaller}\n\n1. Make an empty folder on your computer to make the Project in\n\n\\\n\n2. In RStudio, click \"Project: (None)\" in the top right corner\n - \"New Project...\" ``{=html} \"Existing Directory\" ``{=html} \"Browse...\" ``{=html} find the folder you made in [step 1]{.pink}\n\n\\\n\n3. If the name of the folder you made in [step 1]{.pink} shows up in the top right corner of RStudio, it worked!\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n\n\n
\n\n## [R Scripts]{.orange} {.smaller}\n\n- A code file is called a \"script\"\n - [Microsoft Word]{.orange} **:** [document]{.purple} -- [R]{.orange} **:** [script]{.purple}\n\n\\\n\n. . .\n\n- Lines of code written in scripts can be re-used\n - I.e., \"[re-run]{.orange}\"\n\n\\\n\n. . .\n\n- Scripts can be run line-by-line manually or run all at once\n - Running all at once = \"[sourcing]{.orange}\" a script\n\n## [Practice: Create an R Script]{.pink} {.smaller}\n\n- Create an R script\n - \"File\" ``{=html} \"New File\" ``{=html} \"R Script\"\n\n\\\n\n- Once created, save it as \"316_week1.R\"\n - \"File\" ``{=html} \"Save\"\n\n\\\n\n- Note that the save location defaults to inside of your RStudio Project\n - Another reason to use Projects!\n\n\n## [Base R Fundamentals]{.orange} {.smaller}\n\n- \"Base R\" includes a set of core functions that are broadly useful\n - Reading in a CSV, taking an average, etc.\n\n\\\n\n. . .\n\n- R's real power comes from \"[packages]{.purple}\" with more advanced functions\n - These packages are _not_ in base R\n - We'll begin to cover these [next week]{.blue}\n\n\\\n\n. . .\n\n- Base R is really useful in its own right!\n\n## [Objects and Assignment]{.orange} {.smaller}\n\n- You can do math in R directly\n\n\\\n\n. . .\n\n- More useful to use \"objects\" instead of direct values\n - [object]{.purple} = alias for a value / set of values\n\n\\\n\n. . .\n\n- When we create an object we [assign]{.purple} some value(s) to the object\n - Uses the [assignment operator]{.orange} (`<-`)\n - Keyboard shortcut: Alt + hyphen\n\n\\\n\n. . .\n\n- After you create an object, you can use it in place of its value!\n\n## [Object Example]{.gold}\n\nWe can make an object like so:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmy_obj <- 52\n```\n:::\n\n\n\\\n\n. . . \n\nThen when we use the name of the object later...\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmy_obj\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 52\n```\n\n\n:::\n:::\n\n\n\\\n\n... we get the value we assigned to it!\n\n## [Objects Continued]{.orange} {.smaller}\n\n- Useful because it lets you _flexibly_ code \n\n\\\n\n. . .\n\n- If you know that an object only contains numbers, you can do general operations that work for any number\n\n\\\n\n. . .\n\n- If you had to use the actual value every time, complex operations would be _brutal_ to code\n\n## [Running Code]{.orange} {.smaller}\n\n- How do you 'actually' run code?\n\n\\\n\n- You have two options!\n 1. Click the \"Run\" button in the top right of the \"Source\" pane of RStudio\n 2. Use the keyboard shortcut Command + Return (or for Windows users, Control + Return)\n\n\\\n\n- Note that either option requires that you clicked somewhere in the line you want to run\n - Lines are numbered on the left of the \"Source\" pane\n\n## [Practice: Object Assignment]{.pink} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### Arithmetic\n\n:::\n:::{.column width=\"50%\"}\n\n#### Object Practice\n\n:::\n::::\n\n## [Practice: Object Assignment]{.pink} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### Arithmetic\n\n- Calculate 576 / 3\n\n\\\n\n- Calculate (258 * 4) / 13\n\n\\\n\n- Calculate 2 + 2 - 4\n\n:::\n:::{.column width=\"50%\"}\n\n#### Object Practice\n\n:::\n::::\n\n## [Practice: Object Assignment]{.pink} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### Arithmetic\n\n- Calculate 576 / 3\n\n\\\n\n- Calculate (258 * 4) / 13\n\n\\\n\n- Calculate 2 + 2 - 4\n\n:::\n:::{.column width=\"50%\"}\n\n#### Object Practice\n\n- Create an object called `yy` with a value of 55\n - _Hint_: remember to use the assignment operator (`<-`)\n\n\\\n\n- Create an object called `my_char` with a value of \"text\"\n - What happens when you don't include quotation marks around \"text\"?\n\n:::\n::::\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n\n\n
\n\n## [Code Comments]{.orange} {.smaller}\n\n- Coding languages all allow [comments]{.purple}\n\n\\\n\n. . .\n\n- Comments are _non-coding_ lines that explain what the code is doing in a human-readable way\n\n\\\n\n. . .\n\n- In R, comment lines start with a hashtag (`#`)\n\n\\\n\n. . .\n\n- Comments allow others to read your code and understand what is happening\n - Great for _future you!!!_\n\n## [Code Comments]{.orange}\n\n\n\n
\n\n## [Code Example]{.gold}\n\nHere are some example comments:\n\n```{.r code-line-numbers=\"|1,4\"}\n# Make an object with a value of 77\nq <- 77\n\n# Divide it by 7 and assign to a new object\np <- q / 7\n```\n\n\\\n\n. . .\n\n- Any questions about comments before we continue?\n\n## [Practice: Comments]{.pink} {.smaller}\n\n- Add a comment at the top of your script with the course number and your name\n\n\\\n\n. . .\n\n- For each line of code you wrote earlier:\n - Add a comment line _above it_ explaining what that line does\n - Use wording that makes sense to you!\n\n\\\n\n. . .\n\n- This lets your class notes be included directly with your code!\n - Though you're of course welcome to take notes (or not) using whatever medium you prefer\n\n## [Functions]{.orange} {.smaller}\n\n- Programming wouldn't be useful if you needed to do everything by hand\n\n\\\n\n. . .\n\n- Programming languages include [functions]{.purple} to avoid arithmetic\n - Someone else has done the manual labor and made an object **of that _process_**!\n\n\\\n\n. . .\n\n- You can then use the function instead of doing the math yourself\n\n\\\n\n. . .\n\n- Functions use [arguments]{.purple} to let users specify what they can modify\n\n## [Function Example]{.gold} {.smaller}\n\n- There is a function called `round` that rounds decimals either up or down\n\n\\\n\n. . .\n\n- `round` has two arguments:\n - `x` = the number(s) to round\n - `digits` = the number of digits to which to round `x`\n\n\\\n\n\n. . .\n\n- Let's look at an example\n\n. . . \n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Round a decimal to 2 digits\nround(x = 62.82379110, digits = 2)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 62.82\n```\n\n\n:::\n:::\n\n\n## [Function Help]{.gold}\n\n- Functions include help files built into R to help you use them!\n\n\\\n\n. . .\n\n- Can be accessed by typing a `?` and then the function name\n - E.g., `?mean`\n\n\\\n\n. . . \n\n- Type (and run) `?round` and see what happens\n\n## [Practice: Functions]{.pink} {.smaller}\n\n- Base R includes an object called `pi` that includes the first 6 digits of π\n\n\\\n\n. . .\n\n- Using the `round` function, round `pi` to 3 digits\n\n\\\n\n. . .\n\n- Consult the help file for the square root function (`sqrt`)\n - What argument(s) does `sqrt` expect?\n\n\\\n\n. . .\n\n- What is the square root of pi?\n\n\\\n\n. . .\n\n- Don’t forget to add comments to each line in your script!\n\n## [Vectors and Concatenation]{.orange} {.smaller}\n\n- You can also make objects that contain several values\n - This type of object is called a [vector]{.purple}\n\n\\\n\n. . .\n\n- You can make vectors using \"[concatenation]{.purple}\"\n - \\[con-CAT-eh-nation\\]\n\n\\\n\n. . .\n\n- R does this with the `c` function\n - You need commas between each [element]{.orange} of a vector\n - E.g., `my_vector <- c(1, 2, 3, 4)`\n\n## [Object Classes]{.orange} {.smaller}\n\n- Objects have a [class]{.purple} that dictates what operations they support\n\n\\\n\n. . .\n\n- Math can only be done on \"numeric\" objects\n\n\\\n\n. . .\n\n- Text can be stored in \"character\" objects\n - But math can’t be done on characters!\n\n\\\n\n. . .\n\n- You can check an object's current class with the `class` function\n - E.g., `class(my_object)`\n\n## [Object Classes Cont.]{.orange}\n\n- There are _many_ classes\n - Most of which are beyond the scope of this class\n\n\\\n\n. . .\n\n- Let's focus on 3 fundamental classes\n 1. [Numeric]{.purple} = numbers\n 2. [Character]{.purple} = text\n 3. [Factor]{.purple} = text but defines categorical groups\n\n## [Class Coercion]{.orange} {.smaller}\n\n- You can force (a.k.a. [coerce]{.purple}) an object from one class to another\n\n\\\n\n. . .\n\n- The simplest functions to do this are all `as.\n\n
\n\n## [Upcoming Due Dates]{.blue} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Due before lecture\n\n#### (By midnight)\n\n- Homework \\#1\n- Read the British Ecological Society's [Reproducible Code Guide](https://www.britishecologicalsociety.org/wp-content/uploads/2017/12/guide-to-reproducible-code.pdf) (p.1-12)\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Due before lab\n\n#### (By midnight)\n\n- Muddiest Point \\#1\n- Read the British Ecological Society's [Reproducible Code Guide](https://www.britishecologicalsociety.org/wp-content/uploads/2017/12/guide-to-reproducible-code.pdf) (p.20-25)\n\n:::\n\n::::\n\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/materials/slides_1b.qmd b/materials/slides_1b.qmd index c9536f1..39c9a07 100644 --- a/materials/slides_1b.qmd +++ b/materials/slides_1b.qmd @@ -51,7 +51,7 @@ After today's session you will be able to: :::{.incremental} -- Did anyone _not_ get [R]{.blue} installed? +- Did anyone _not_ get [R]{.orange} installed? \ @@ -66,10 +66,24 @@ After today's session you will be able to: ## [R versus RStudio]{.orange} {.smaller} ::::{.columns} +:::{.column width="50%"} + +![](images/logo-r.png){.absolute top=100 left=150 width="15%" fig-alt="R logo"} + +::: +:::{.column width="50%"} + +![](images/logo-rstudio.png){.absolute top=100 left=580 width="35%" fig-alt="RStudio logo"} + +::: +:::: + +## [R versus RStudio]{.orange} {.smaller} +::::{.columns} :::{.column width="50%"} -![](images/logo-r.png){.absolute top=100 left=150 width="15%"} +![](images/logo-r.png){.absolute top=100 left=150 width="15%" fig-alt="R logo"} \ \ \ @@ -82,10 +96,34 @@ After today's session you will be able to: - Unlikely to matter for this course! ::: +:::{.column width="50%"} + +![](images/logo-rstudio.png){.absolute top=100 left=580 width="35%" fig-alt="RStudio logo"} + +::: +:::: + +## [R versus RStudio]{.orange} {.smaller} + +::::{.columns} +:::{.column width="50%"} + +![](images/logo-r.png){.absolute top=100 left=150 width="15%" fig-alt="R logo"} +\ +\ +\ +\ + +- Access to fundamental R operations +- Not aesthetically pleasing + - In my opinion `r fontawesome::fa(name = "smile", a11y = "sem")` +- Faster for _very_ complex operations + - Unlikely to matter for this course! +::: :::{.column width="50%"} -![](images/logo-rstudio.png){.absolute top=100 left=580 width="35%"} +![](images/logo-rstudio.png){.absolute top=100 left=580 width="35%" fig-alt="RStudio logo"} \ \ \ @@ -94,17 +132,16 @@ After today's session you will be able to: - Access to fundamental R operations - Allows custom aesthetics - Has quality-of-life improvements -- Technically an "[IDE]{.purple}" +- Technically an "[IDE]{.orange}" - Integrated Developer Environment ::: - :::: ## [RStudio Layout]{.orange} {.smaller} -- Layout consisting of four [panes]{.blue} - - Each pane has [tabs]{.gold} that do different things +- Layout consisting of four [panes]{.purple} + - Each pane has [tabs]{.orange} that do different things - You can customize which tabs are in each pane! - To customize options: - In top menu, click "Tools" @@ -112,15 +149,16 @@ After today's session you will be able to: - Select "Pane Layout" - I recommend the following layout: -![](images/rstudio-layout.png){.absolute top=280 left=600 width="40%"} +![](images/rstudio-layout.png){.absolute top=260 left=600 width="40%" fig-alt="Image of four squares arranged in a 2x2 grid. The top left is titled 'source', the bottom left is titled 'console' and the two right squares are labeled 'your choice'"} ## [Practice: Choose Your Colors]{.pink} - Tools `r fontawesome::fa("arrow-right", a11y = "sem")` Global Options `r fontawesome::fa("arrow-right", a11y = "sem")` Appearance -- Look through some of the available color schemes +\ -- Feel free to share the name of your favorites! +- Look through some of the available color schemes + - Feel free to share the name of your favorites! \ @@ -133,7 +171,7 @@ After today's session you will be able to: :::{.incremental} -- Projects are a special way of marking a folder on your computer for R +- [Projects]{.purple} are a special way of marking a folder on your computer for R \ @@ -171,9 +209,7 @@ After today's session you will be able to: \ 2. In RStudio, click "Project: (None)" in the top right corner - - Choose "New Project..." - - Pick "Existing Directory" - - Click "Browse..." and find the folder you made in [step 1]{.pink} + - "New Project..." `r fontawesome::fa("arrow-right", a11y = "sem")` "Existing Directory" `r fontawesome::fa("arrow-right", a11y = "sem")` "Browse..." `r fontawesome::fa("arrow-right", a11y = "sem")` find the folder you made in [step 1]{.pink} \ @@ -190,17 +226,21 @@ After today's session you will be able to: ## [R Scripts]{.orange} {.smaller} - A code file is called a "script" - - [Microsoft Word]{.blue}:[document]{.gold} -- [R]{.blue}:[script]{.gold} + - [Microsoft Word]{.orange} **:** [document]{.purple} -- [R]{.orange} **:** [script]{.purple} \ +. . . + - Lines of code written in scripts can be re-used - - I.e., re-"run" + - I.e., "[re-run]{.orange}" \ +. . . + - Scripts can be run line-by-line manually or run all at once - - Running all at once = "sourcing" a script + - Running all at once = "[sourcing]{.orange}" a script ## [Practice: Create an R Script]{.pink} {.smaller} @@ -225,12 +265,16 @@ After today's session you will be able to: \ -- R's real power comes from "packages" with more advanced functions +. . . + +- R's real power comes from "[packages]{.purple}" with more advanced functions - These packages are _not_ in base R - - We'll begin to cover these next week + - We'll begin to cover these [next week]{.blue} \ +. . . + - Base R is really useful in its own right! ## [Objects and Assignment]{.orange} {.smaller} @@ -239,17 +283,23 @@ After today's session you will be able to: \ -- More useful to use [objects]{.blue} instead of direct values - - [object]{.blue} = alias for a value / set of values +. . . + +- More useful to use "objects" instead of direct values + - [object]{.purple} = alias for a value / set of values \ -- When we create an object we [assign]{.gold} some value(s) to the object +. . . + +- When we create an object we [assign]{.purple} some value(s) to the object - Uses the [assignment operator]{.orange} (`<-`) - Keyboard shortcut: Alt + hyphen \ +. . . + - After you create an object, you can use it in place of its value! ## [Object Example]{.gold} @@ -276,22 +326,55 @@ my_obj ... we get the value we assigned to it! -## [Objects Continued]{.orange} +## [Objects Continued]{.orange} {.smaller} - Useful because it lets you _flexibly_ code \ -- If you know that an object only contains numbers, you can do general operations that work for any number object +. . . + +- If you know that an object only contains numbers, you can do general operations that work for any number + +\ + +. . . + +- If you had to use the actual value every time, complex operations would be _brutal_ to code + +## [Running Code]{.orange} {.smaller} + +- How do you 'actually' run code? + +\ + +- You have two options! + 1. Click the "Run" button in the top right of the "Source" pane of RStudio + 2. Use the keyboard shortcut Command + Return (or for Windows users, Control + Return) \ -- If you had to use the actual value every time, complex operations would be brutal to code +- Note that either option requires that you clicked somewhere in the line you want to run + - Lines are numbered on the left of the "Source" pane ## [Practice: Object Assignment]{.pink} {.smaller} ::::{.columns} +:::{.column width="50%"} + +#### Arithmetic + +::: +:::{.column width="50%"} +#### Object Practice + +::: +:::: + +## [Practice: Object Assignment]{.pink} {.smaller} + +::::{.columns} :::{.column width="50%"} #### Arithmetic @@ -307,7 +390,31 @@ my_obj - Calculate 2 + 2 - 4 ::: +:::{.column width="50%"} +#### Object Practice + +::: +:::: + +## [Practice: Object Assignment]{.pink} {.smaller} + +::::{.columns} +:::{.column width="50%"} + +#### Arithmetic + +- Calculate 576 / 3 + +\ + +- Calculate (258 * 4) / 13 + +\ + +- Calculate 2 + 2 - 4 + +::: :::{.column width="50%"} #### Object Practice @@ -321,30 +428,43 @@ my_obj - What happens when you don't include quotation marks around "text"? ::: - :::: +## [Temperature Check]{.purple} + +#### How are you Feeling? + ++ +
+ ## [Code Comments]{.orange} {.smaller} -- Coding languages all allow [comments]{.gold} +- Coding languages all allow [comments]{.purple} \ +. . . + - Comments are _non-coding_ lines that explain what the code is doing in a human-readable way \ +. . . + - In R, comment lines start with a hashtag (`#`) \ +. . . + - Comments allow others to read your code and understand what is happening - Great for _future you!!!_ ## [Code Comments]{.orange}- +
## [Code Example]{.gold} @@ -359,6 +479,12 @@ q <- 77 p <- q / 7 ``` +\ + +. . . + +- Any questions about comments before we continue? + ## [Practice: Comments]{.pink} {.smaller} - Add a comment at the top of your script with the course number and your name @@ -376,23 +502,30 @@ p <- q / 7 . . . - This lets your class notes be included directly with your code! + - Though you're of course welcome to take notes (or not) using whatever medium you prefer ## [Functions]{.orange} {.smaller} -- Programming wouldn't be useful if you could only use arithmetic +- Programming wouldn't be useful if you needed to do everything by hand \ -- Programming languages include [functions]{.blue} to avoid arithmetic - - Someone else has done the arithmetic and made an object **of that process**! +. . . + +- Programming languages include [functions]{.purple} to avoid arithmetic + - Someone else has done the manual labor and made an object **of that _process_**! \ +. . . + - You can then use the function instead of doing the math yourself \ -- Functions use [arguments]{.orange} to let users specify what they can modify +. . . + +- Functions use [arguments]{.purple} to let users specify what they can modify ## [Function Example]{.gold} {.smaller} @@ -400,6 +533,8 @@ p <- q / 7 \ +. . . + - `round` has two arguments: - `x` = the number(s) to round - `digits` = the number of digits to which to round `x` @@ -407,6 +542,8 @@ p <- q / 7 \ +. . . + - Let's look at an example . . . @@ -424,6 +561,8 @@ round(x = 62.82379110, digits = 2) \ +. . . + - Can be accessed by typing a `?` and then the function name - E.g., `?mean` @@ -431,7 +570,7 @@ round(x = 62.82379110, digits = 2) . . . -Type and run `?round` and see what happens +- Type (and run) `?round` and see what happens ## [Practice: Functions]{.pink} {.smaller} @@ -439,6 +578,8 @@ Type and run `?round` and see what happens \ +. . . + - Using the `round` function, round `pi` to 3 digits \ @@ -450,6 +591,8 @@ Type and run `?round` and see what happens \ +. . . + - What is the square root of pi? \ @@ -465,69 +608,90 @@ Type and run `?round` and see what happens \ -- You can make vectors using "concatenation" +. . . + +- You can make vectors using "[concatenation]{.purple}" - \[con-CAT-eh-nation\] \ -- R does this with a lower case `c` and parentheses +. . . + +- R does this with the `c` function - You need commas between each [element]{.orange} of a vector - E.g., `my_vector <- c(1, 2, 3, 4)` ## [Object Classes]{.orange} {.smaller} -- Objects have a [class]{.blue} that dictates what operations they support +- Objects have a [class]{.purple} that dictates what operations they support \ +. . . + - Math can only be done on "numeric" objects \ +. . . + - Text can be stored in "character" objects - But math can’t be done on characters! \ +. . . + - You can check an object's current class with the `class` function - E.g., `class(my_object)` ## [Object Classes Cont.]{.orange} - There are _many_ classes + - Most of which are beyond the scope of this class \ -- Let's focus on 4 fundamental classes - - Numeric = numbers - - Integer = numbers without _any_ decimal places - - Character = text - - Factor = text but defines categorical groups +. . . + +- Let's focus on 3 fundamental classes + 1. [Numeric]{.purple} = numbers + 2. [Character]{.purple} = text + 3. [Factor]{.purple} = text but defines categorical groups ## [Class Coercion]{.orange} {.smaller} -- You can force (a.k.a. [coerce]{.pink}) an object from one class to another +- You can force (a.k.a. [coerce]{.purple}) an object from one class to another \ +. . . + - The simplest functions to do this are all `as.