Keeping R code DRY with functions: Don't repeat yourself! (CC096)
Channel: Riffomonas Project
Date: 2021-04-26

Don't repeat yourself or the DRY principle is one of the best practices to writing good R. When you repeat the same block of code you risk introducing errors. The solution is to write functions in R. Functions allow you to avoid repeating yourself and then you can call the function whenever you want to use that chunk of code. Pat demonstrates this by generating receiver operator characteristic (ROC) curves for data from a paper his lab published a few years ago.

Pat will use RStudio and functions from the #ggplot2 package and other packages from #R and the tidyverse including #glue, #function, and more. The accompanying blog post can be found at https://www.riffomonas.org/code_club/2021-04-26-roc-curve.

Do you have a figure that you would like to receive a critique or help improving? Let me know and I'd be happy to arrange a guest appearance!

If you're interested in taking an upcoming 3 day R workshop, email me at riffomonas@gmail.com!

R: https://r-project.org
RStudio: https://rstudio.com
Raw data: https://github.com/riffomonas/raw_data/releases/latest
Workshops: https://www.mothur.org/wiki/workshops

You can also find complete tutorials for learning R with the tidyverse using...
Microbial ecology data: https://www.riffomonas.org/minimalR/
General data: https://www.riffomonas.org/generalR/

0:00 Introduction
2:27 Dynamically filtering and recoding data w/ glue
6:06 Setting up a map function to create a column
7:31 Creating a function
14:05 Deploying function in map with unnest
15:38 Creating another function
16:52 Deploying for all three comparisons
18:39 Plotting ROC curves w/ geom_line
22:20 Recap