Applying the DRY principle to repetitive R code (CC132)
Do you have R scripts where you repeat the same chunk of code over and over? You need to learn about the DRY principle! DRY stands for Don’t Repeat Yourself. Following this principle will make your code easier to manage and help you to avoid propogating errors across your project. Check out this episode to see how Pat uses the DRY principle to minimize duplication and then demonstrates how to expand his code using the DRY principle.
In this episode, Pat will use functions from the
mikropml R package and data handling functions from
dplyr in RStudio.
You can browse the state of the repository at the
If you haven’t been following along, you can get caught up by doing the following:
- (windows) Install the Ubuntu Linux BASH shell for Windows 10
- (mac) Install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install git
- To get to where we are at the beginning of this episode (you won’t have the same issue numbers at Pat)…
- Set up a GitHub account
- Create a new GitHub repository
- Call it “mikropml_demo”
- Make it Public
- Don’t check the box next to “Initialize this repository with a README”
- Click the green “Create repository” button
Go to your command line and enter the following replacing
<your_github_id>with your GitHub user id
git clone email@example.com:SchlossLab/mikropml_demo.git cd mikropml_demo git reset --hard 958530ddf30b7e86f3aa38ac53f0010c344b7fae git remote set-url origin firstname.lastname@example.org:<your_github_id>/mikropml_demo.git git push -u origin master
- Return to GitHub and refresh your browser.
- Go to the
mikropml_demodirectory on your computer and double click on the
mikropml_demo.Rprojicon. This will launch RStudio and you’ll be good to go.