4 Introduction to course
The slides contain speaking notes that you can view by pressing βSβ on the keyboard.
4.1 The Big Picture
This section provides a bigger-picture view of what we will be doing, why we want to do it, how we will go about doing it, and what it will look like in the end.
Our big picture aim is to create a data analysis project that:
- Makes it easier for collaborators and others to contribute directly
- Explicitly includes the processing and analysis steps (as code), so they are reproducible
- Incorporates general-purpose tools that simplify the use or switching of statistical analysis methods
All of this will be exemplified through a simple analysis of a lipidomics dataset during the course.
Where will we start and where will we end, in a more βtangibleβ way? The most tangible things are the folders and files on our computers. The folder and file structures below show where we start and where we end, so you can hopefully get a better understanding of how things will look.
Initial project structure
Right now, your initial project structure should look like this:
LearnR3
βββ data/
β βββ lipidomics.csv
β βββ README.md
βββ data-raw/
β βββ README.md
β βββ nmr-omics/
β β βββ lipidomics.xlsx
β β βββ README.txt
β βββ nmr-omics.R
βββ doc/
β βββ README.md
β βββ learning.qmd
β βββ report.Rmd
βββ R/
β βββ functions.R
β βββ README.md
βββ .gitignore
βββ DESCRIPTION
βββ LearnR3.Rproj
βββ README.md
βββ TODO.md
Final project structure
At the end of this course, it should look something like:
LearnR3
βββ _targets/
β βββ meta/
β β βββ meta
β βββ objects/
β βββ user/
β βββ workspaces/
βββ data/
β βββ lipidomics.csv
β βββ README.md
βββ data-raw/
β βββ README.md
β βββ nmr-omics/
β β βββ lipidomics.xlsx
β β βββ README.txt
β βββ nmr-omics.R
βββ doc/
β βββ _targets.yaml
β βββ README.md
β βββ learning.html
β βββ learning.Rmd
βββ R/
β βββ functions.R
β βββ README.md
βββ .gitignore
βββ _targets.R
βββ DESCRIPTION
βββ LearnR3.Rproj
βββ README.md
Why do we structure it this way?
- To follow βproject-orientedβ workflows (covered in Chapter 5).
- To follow some standard conventions in R, like having a
DESCRIPTION
file (which is important for Chapter 5). - To keep types of files separate, like raw data raw and in the
data-raw/
folder, R scripts/functions in theR/
folder, and documents like R Markdown / Quarto files indoc/
.
This structure also supports our workflow and processes throughout the course, which will be to:
- Track package dependencies in the
DESCRIPTION
file. - Follow a βfunction-orientedβ workflow, where we use R Markdown / Quarto (
doc/learning.qmd
) to write and test out code, convert it into a function, test it, and then move it intoR/functions.R
.- We develop functions in the
learning.qmd
file to make it a bit easier to quickly test the code and make sure it works before moving it over into a more formal location and structure. Think of this file as a sandbox to test out and play with code, without fear of messing things up. - We also test the code in
learning.qmd
because, from a teaching and learning perspective, itβs easier to integrate text and comments with the code during the code-alongs in Markdown files. - We keep functions in a separate
functions.R
file because we will frequentlysource()
from it as we prototype and test out code in thelearning.qmd
file. This also creates a clear separation between βfinalizedβ code and prototype code.
- We develop functions in the
- Use a combination of restarting R with Ctrl-Shift-F10Ctrl-Shift-F10 or with the Palette (Ctrl-Shift-PCtrl-Shift-P, then type βrestartβ) (or
Session -> Restart R
) and usingsource()
(Ctrl-Shift-SCtrl-Shift-S or with the Palette (Ctrl-Shift-PCtrl-Shift-P, then type βsourceβ) while inR/functions.R
) to run the functions inside ofR/functions.R
.- We restart R to ensure that the R workspace is completely clear. For reproducibility, we should always aim to work from a βclean plateβ.
- Keep code readable by having the formatting/styling of our code fixed automatically.
- For each βoutputβ (like a figure or a table) in a paper, write one or more functions to generate it and include each function as steps or βtargetsβ in a pipeline. Use the pipeline to track and order the steps in the data analysis.
- Write accompanying text (which outside this course could be a full paper) for the analysis in Markdown so we can easily and quickly regenerate reports for rapid dissemination.
- Automatically reformat Markdown text into a standard, more readable format.
- Whenever we complete a task, we add and commit those file changes to save them in the Git history with Ctrl-Alt-MCtrl-Alt-M or with the Palette (Ctrl-Shift-PCtrl-Shift-P, then type βcommitβ).
- We use Git to keep track of what changes were made to the files, when, and why. This keeps our work transparent and makes it easier to share the code by uploading it to GitHub. Version control aligns with the philosophy of reproducible science and should be a standard practice (it usually isnβt, which is why we practice it here).
Many of these βproject setupβ tasks can be time-consuming, difficult and confusing - and this is before youβve even gotten to the analysis phase of your work.
A good analogy for these first steps is when skyscrapers are built: Watching construction on these projects makes it feel like it takes forever for them to finally start going up and adding floors. But once they start adding floors, it goes up so fast! Thatβs because a lot of the main work is in building up the foundation of the building, so that it is strong and stable. This is the same with analysis projects, the first phase feels like nothing is βmovingβ but you are building the foundation to everything that comes after.
Throughout the many times weβve taught this course and others, we get asked a lot of questions. We have a Frequently Asked Questions page for keeping track of some of these questions. Check out this page, maybe your question has already been answered!
If you want to get help virtually or after the course, you can join the Discord channel where you can ask questions in the questions-or-advice
text channel.
During the course, we will be writing and coding mostly in the doc/learning.qmd
file. We will also be regularly deleting the content within the file to keep things clean and easier for you, but importantly for the instructors. If or when you encounter an error or problem and there is a lot of the code kept in the file, often the problem is due to the left over code rather than an actual problem with the code you are writing. So when the helpers or instructors come to help, it makes it easier for us to help you when there is less code to look through and debug.
But, we know you may want to keep some notes as you work in the course. So we suggest you create a new file called notes.qmd
or something similar in the doc/
folder and either:
- Write notes in the
doc/learning.qmd
file and then copy them over to yournotes.qmd
file when we tell you to delete everything, or - Write notes directly in your
doc/notes.qmd
file and keep it open while you work in thedoc/learning.qmd
file.
When youβre ready to continue, place the sticky/paper hat on your computer to indicate this to the instructor π π©
4.2 Small fixes
We need to add a Title:
field to the DESCRIPTION
file in order for other sections of the course to work properly. Run this bit of code:
Then delete the doc/report.Rmd
file: