Skip to content

Commit 2f7a3e8

Browse files
Updating htmls and pngs with new render
1 parent 724b584 commit 2f7a3e8

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"hash": "5b4b8599afe7beae727945d381ac6757",
3+
"result": {
4+
"markdown": "---\ntitle: \"Creating a Basic For Loop\"\nauthor: \n - name: \"Daphne Virlar-Knight\"\n orcid: \"0000-0003-3708-6154\"\n\nabstract: \"Learn how to write a `for` loop when you're dealing with numeric values.\"\n\ndate: \"Sep 5, 2022\"\ndate-format: MMMM D, YYYY\n\nimage: ../images/basic-for-loop.png\n\ncategories: iteration\n---\n\n\n------------------------------------------------------------------------\n\nOne of the key tenets of programming is the DRY principle: **D**on't **R**epeat **Y**ourself. Essentially, this boils down to not repeating your code ad nauseum to change only one or two things. This is particularly useful for computationally intensive workflows that would require changing tens or hundreds of items. That's where the `for` loop comes into play.\n\nFirst, let's look at the basic syntax of a `for` loop. When you write one, you're telling the computer \"run this piece of code (`statement`) some number of times (`range_of_values`) in this spot (`value`).\"\n\n::: callout-tip\n#### Syntax\n\n\n::: {.cell}\n\n```{.r .cell-code}\nfor(value in range_of_values){\n statement \n print(statement) \n}\n```\n:::\n\n:::\n\nAnd that's it! Congratulations, now you know the syntax for a basic `for` loop! Now let's see it in practice. Now let's put it into practice. \n\n\n::: callout-note\nLet's say Europe is experiencing a heat wave, but I'm not quite grasping the context of how bad it is because I'm not familiar with the Celsius temperature scale. I know the average temperatures are somewhere between 35-40&deg;C, so I'll write a quick function to convert the temperatures into Fahrenheit! But that spans over five numbers, and writing that function out five individual times goes against the DRY principle. This gives us the perfect excuse to write a `for` loop. \n\nThe equation for converting temperatures from Celsius to Fahrenheit is the following. $$(deg C*1.8) + 32$$ \nKnowing that conversion formula, here's how I'd write my `for` loop:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nfor(degC in 35:40){\n # Write your statement. Make sure the output of the statement is assigned to an object,\n # otherwise R will only remember the very last conversion value. \n fahr <- (degC*1.8) + 32\n \n # Print the outputs\n print(fahr)\n}\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 95\n[1] 96.8\n[1] 98.6\n[1] 100.4\n[1] 102.2\n[1] 104\n```\n:::\n:::\n\n\nYep, that's pretty toasty! \n:::\n\nFor more help on `for` loops and other iterative processes, make sure to check out the [R for Data Science](https://r4ds.had.co.nz/iteration.html#iteration) book by Hadley Wickham and Garrett Grolemund!",
5+
"supporting": [],
6+
"filters": [
7+
"rmarkdown/pagebreak.lua"
8+
],
9+
"includes": {},
10+
"engineDependencies": {},
11+
"preserve": {},
12+
"postProcess": true
13+
}
14+
}
196 KB
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"hash": "9086eb3e680f668c2aaba87daa6eb62b",
3+
"result": {
4+
"markdown": "---\ntitle: \"Using Icons in Figures with {ggsvg}\"\nauthor:\n - name: \"Haley Epperly Fox\"\n orcid: \"0000-0001-6102-4784\"\n\nabstract: \"Learn how to insert icons into your plots using the `{ggsvg}` package.\"\n\ndate: Sep 5, 2022\ndate-format: MMMM D, YYYY\n\nimage: ../images/ggsvg-penguin.svg\n\ncategories: graph aesthetics\nbibliography: references.bib\n---\n\n\n------------------------------------------------------------------------\n\nHave you ever seen a figure with cute little icons representing data points and thought how you would love to recreate that? I'm here to tell you that you can and the `ggsvg` package makes it super simple.\n\nAll you have to do is:\n\n1. Load in the `ggsvg` package\n\n2. Figure out which icon you want to use from the ones available [here](https://www.svgrepo.com/)\n\n3. Save the url for your chosen icon to read into your gpplot code\n\n4. Use `geom_point_svg` in your ggplot code referencing your saved url [@ggsvg]\n\nLet's run through an example with the `palmerpenguins` dataset [@palmerpenguins]. Checkout the package [website](https://allisonhorst.github.io/palmerpenguins/) for some more info and fun artwork! We're going to subset these data to only include female Chinstrap penguins so that we have fewer data points to plot.\n\n::: callout-note\n#### Example\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# load libraries\nlibrary(palmerpenguins)\nlibrary(tidyverse)\nlibrary(ggsvg)\n```\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\n# find the icon you want and save the url for later reference following this format\nsvg_url <- 'https://www.svgrepo.com/download/133788/penguin.svg'\nsvg_txt <- paste(readLines(svg_url), collapse = \"\\n\")\n\n# filter the penguin data to only female Chinstrap penguins\nchinstrap <- penguins %>% \n filter(species == \"Chinstrap\", sex == \"female\")\n\n# plot the data with the selected icon used instead of points\nggplot(chinstrap) + \n geom_point_svg(aes(body_mass_g, flipper_length_mm), svg = svg_txt) + \n labs(x = \"Body Mass (g)\", y = \"Flipper Length (mm)\", \n title = \"Comparing body mass and flipper length of female Chinstrap penguins\") +\n theme_classic()\n```\n\n::: {.cell-output-display}\n![](ggsvg_files/figure-html/plot-1.png){width=672}\n:::\n:::\n\n:::\n",
5+
"supporting": [
6+
"ggsvg_files"
7+
],
8+
"filters": [
9+
"rmarkdown/pagebreak.lua"
10+
],
11+
"includes": {},
12+
"engineDependencies": {},
13+
"preserve": {},
14+
"postProcess": true
15+
}
16+
}
196 KB
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"hash": "289c6544e2d96f01112dee44a2943b6b",
3+
"result": {
4+
"markdown": "---\ntitle: \"Looping through Non-Numbers\"\nauthor: \n - name: \"Nick J. Lyon\"\n orcid: \"0000-0003-3905-1078\"\n\nabstract: \"How to write `for` loops when your sequence doesn't contain numeric values.\" \n\ndate: \"Sep 1, 2022\"\ndate-format: MMMM D, YYYY\n\nimage: ../images/looping-through-non-numbers.png\n\ncategories: iteration\n---\n\n\n---\n\n`for` loops in R are a great way of repeating the same workflow iteratively rather than manually copy/pasting a given workflow for each case. `for` loops are so named because their syntax asks you for which groups you want to repeat the given workflow. The fundamental syntax is as follows:\n\n:::callout-tip\n#### Syntax\n\n::: {.cell}\n\n```{.r .cell-code}\nfor(single_group in all_groups){\n ...workflow with each \"single_group\"...\n}\n```\n:::\n\n:::\n\nIt is common to learn `for` loops by giving numbers to the `for` function and then conducting some sort of algebraic modification in the curly braces (`{...}`) after the `for`. For instance, we could square every number between 1 and 5 using a `for` loop.\n\n:::callout-note\n#### Example\n\n::: {.cell}\n\n```{.r .cell-code}\nfor(j in 1:5){\n # Square \"j\"\n result <- j^2\n # Print the result in each loop\n print(result)\n}\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 1\n[1] 4\n[1] 9\n[1] 16\n[1] 25\n```\n:::\n:::\n\n:::\n\nIn each iteration of the loop above (i.e., *for* each value between 1 and 5), `j` becomes the next number in the provided sequence and squares it. At the end of the loop, your environment will have a value called `j` that is equal to 5 and an object called `result` that is equal to 25. This is because `for` loops only retain the final value of whatever passes through them. There are ways of adding each loop's product to a single object so your output contains the results of all iterations of the loop but we will leave that for another time.\n\nWhile using numbers as the inputs for a `for` loop is great, many R users don't realize that **you can also use _characters_!** This can be really useful if you have, for example, a dataset with many groups and you want to fit a linear regression for each level in your group column separately. To demonstrate this, we'll use the `penguins` dataset included in the `palmerpenguins` R package.\n\nThe `penguins` dataset contains individual-level data on three penguin species (run `?penguins` for more specific detail). Let's say that we want to run compare the bill length between male and female penguins *for* each species. For simplicity's sake, we'll use a Student's t-Test and extract only the p value.\n\n:::callout-note\n#### Example\n\n::: {.cell}\n\n```{.r .cell-code}\n# Load the package\nlibrary(palmerpenguins)\n\n# For each species in the dataframe\nfor(sp in unique(penguins$species)){\n \n # Subset the data to the selected species and drop NAs in `sex`\n data_sub <- subset(penguins, species == sp & !is.na(sex))\n \n # Now fit the t-test\n stats <- t.test(data_sub$bill_length_mm ~ data_sub$sex)\n \n # And print the p-value!\n message(\"For species \", sp, \" the p value is \", stats$p.value)\n}\n```\n\n::: {.cell-output .cell-output-stderr}\n```\nFor species Adelie the p value is 4.80108238442492e-15\n```\n:::\n\n::: {.cell-output .cell-output-stderr}\n```\nFor species Gentoo the p value is 1.31503894530191e-14\n```\n:::\n\n::: {.cell-output .cell-output-stderr}\n```\nFor species Chinstrap the p value is 8.91840858173204e-10\n```\n:::\n:::\n\n:::\n\nThis can also be used to loop through the column names of a single dataframe or elements of a list! Supplying characters to a `for` loop can make the mental gymnastics of picturing your loop much simpler so definitely try this in your code!",
5+
"supporting": [],
6+
"filters": [
7+
"rmarkdown/pagebreak.lua"
8+
],
9+
"includes": {},
10+
"engineDependencies": {},
11+
"preserve": {},
12+
"postProcess": true
13+
}
14+
}

0 commit comments

Comments
 (0)