Console
usethis::use_data_raw("cv-risk")You’ll be using a synthetic dataset (generated from real data) during the code-along portion of the workshop. This dataset is an openly licensed dataset from Zenodo with data on basic clinical characteristics and disease status (1). You can check out this dataset on Zenodo for more details about it.
Unlike the intermediate workshop, we will not be doing any processing or cleaning of the research data. We’ll provide a relatively clean and tidy dataset for you to use during this workshop. If you want to see what we did to tidy it up, check out the R script on the GitHub repo. However, just like in the intermediate workshop, you’ll still create a script in data-raw/ that will download the dataset and save it to the data/ folder.
First step, you’ll create this R script by typing and running the following code in the R Console pane while in your AdvancedR3 R Project:
Console
usethis::use_data_raw("cv-risk")This function creates a new folder called data-raw/ in your project (if it didn’t exist already) and creates an R script called cv-risk.R in that folder. The R script should have opened up for you, otherwise, go into the data-raw/ folder and open up the new nmr-omics.R script. The first thing to do is delete all the code in the script. Than, copy and paste the code below into the script. You can run the lines one at a time and see what they do on your own or source() the file all at once. The comments provided give guidance on what the code is doing and why.
cv_risk_path <- here::here("data", "cv-risk.csv")
if (!fs::file_exists(cv_risk_path)) {
download.file(
"https://r-cubed-advanced.rostools.org/data/cv-risk.csv",
destfile = cv_risk_path,
# For in Windows users.
mode = "wb"
)
}After running this code (either line-by-line or via source()), you should now have the cv-risk.csv dataset saved in the data/cv-risk.csv file. The created files should look like this:
AdvancedR3
├── data
│ └── cv-risk.csv
└── data-raw
└── cv-risk.R
The created dataset in data/cv-risk.csv should look like the below when loading it using readr::read_csv(). You don’t need to run this code, this is only to show you what the data looks like.
Rows: 500 Columns: 19
── Column specification ────────────────────────────────────────────────
Delimiter: ","
chr (2): patient_id, gender
dbl (9): age, systolic_blood_pressure, total_cholesterol, high_densi...
lgl (8): smoking, diabetes, hypertension, atrial_fibrillation, famil...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# A tibble: 500 × 19
patient_id gender age systolic_blood_pressure total_cholesterol
<chr> <chr> <dbl> <dbl> <dbl>
1 CARD-00001 men 46 127. 249.
2 CARD-00002 men 54 122. 226
3 CARD-00003 men 47 140. 179.
4 CARD-00004 women 75 163. 267
5 CARD-00005 women 66 129. 199.
6 CARD-00006 women 82 147. 245.
7 CARD-00007 men 67 137. 253.
8 CARD-00008 men 55 125. 196
9 CARD-00009 women 41 90 168.
10 CARD-00010 women 63 175. 246.
# ℹ 490 more rows
# ℹ 14 more variables: high_density_lipoprotein <dbl>,
# low_density_lipoprotein <dbl>, body_mass_index <dbl>, hba1c <dbl>,
# smoking <lgl>, diabetes <lgl>, hypertension <lgl>,
# atrial_fibrillation <lgl>, family_history_cvd <lgl>,
# alcohol_use <lgl>, score2_10y <dbl>, ascvd_pce_10y <dbl>,
# outcome_any_cv_event <lgl>, outcome_death <lgl>
Take a look through the dataset’s Zenodo page if you’d like to learn more about the data, though this isn’t required as we will go through it during the workshop.
You’ll be using Git to track changes made to files in our project. For a normal data analysis project that is tracked by Git, you shouldn’t commit data files. That’s because Git:
Next, run this command to check that everything is setup correctly. You will need to paste this output into the survey at the end.
Console
r3::check_project_setup_advanced()Show folders and files of project:
• Please copy and paste this output into the survey question:
/home/luke/Desktop/AdvancedR3
├── AdvancedR3.Rproj
├── .gitignore
├── DESCRIPTION
├── R/
│ ├── README.md
│ └── functions.R
├── README.md
├── TODO.md
├── data/
│ ├── README.md
│ └── cv-risk.csv
├── data-raw/
│ └── cv-risk.R
└── docs/
├── README.md
├── sandbox.qmd
└── report.qmd
If you encounter an error that says something like “Error in gh::gh()”, you can (potentially) solve it by running gitcreds::gitcreds_set() in the Console as you did in Chapter 9 and then running the r3::check_project_setup_advanced() command again.
The output should look something a bit like the above text. If it doesn’t, start over by deleting everything in the data-raw/ folder except for the data-raw/cv-risk.R script and re-running the script again. If your output looks a bit like the above, than copy and paste the output into the survey question at the end.
We’ve covered how to adhere to a style guide in both the introductory and intermediate workshop, because it is such a useful and powerful tool to easily write more readable code. We’ll expand on it in this workshop because it fits precisely with the theme of collaboration. That’s because, when you’re working on your own and don’t need to worry about anyone seeing your code, there’s a natural temptation to write your code like you might write notes to yourself: scribbled and scrawled down quickly.
But when working with others, how it looks can greatly impact how quickly and easily others are able to read and interpret your code. Multiply all the collaborators on a project with this natural temptation for quick (and potentially sloppy) coding, you can imagine how easy it is for code to massively “drift” towards being poorly formatted, especially when deadlines are approaching.
That’s when “linters” or “stylers” (types of static code analysis tools) become very useful. They scan your code for common mistakes or syntax issues, either listing them for you to fix or automatically fixing them for you. Linters are especially useful in ensuring the code is consistently formatted across the project when collaborating with someone who is less experienced with coding or who contributes occasionally.
To ensure consistent styling without needing to run checks manually, automatic linting/styling can be a great solution. That’s where the styler package comes in!
There are only a few functions in styler that we can to use. The first is used to style a single file with R code in it using styler::style_file(). However, an easier way to style the file we’re currently working on is the “style active file” RStudio addin. We use that through the Command Palette (Ctrl-Shift-PCtrl-Shift-P) and typing “style file”, which should show the “Style active file” option. This is what we will do frequently throughout the workshop.
Let’s try it out. While inside the data-raw/cv-risk.R file, use the Palette (Ctrl-Shift-PCtrl-Shift-P, then type “style file”) to style the file. Notice that no changes were made since the file is already formatted.
You will probably be asked to install a package called miniUI, click “Yes”.
If you wanted to run styler on all the files, you can use:
Console
styler::style_dir()Now that you are done, you can commit the changes by either using the Git interface or by running the below functions in the Console.
With the Git interface, open it with either the Git icon at the top near the menu bar or with Ctrl-Alt-MCtrl-Alt-M or with the Palette (Ctrl-Shift-PCtrl-Shift-P, then type “commit”). While in the interface, select all the current changes by clicking the checkbox beside the files and stage them. Then write a commit message in the text box on the right, something like Download and save data. Click the “Commit” button, and finally close the Git interface.
If you have never used the Git interface before, you can instead copy and paste these R functions to do the same thing. So paste these functions into the Console and run them:
Console
gert::git_add(".")
gert::git_commit("Download and save data")This lists some, but not all, of the code used in the section. Some code is incorporated into Markdown content, so is harder to automatically list here in a code chunk. The code below also includes the code from the exercises.
usethis::use_data_raw("cv-risk")
cv_risk_path <- here::here("data", "cv-risk.csv")
if (!fs::file_exists(cv_risk_path)) {
download.file(
"https://r-cubed-advanced.rostools.org/data/cv-risk.csv",
destfile = cv_risk_path,
# For in Windows users.
mode = "wb"
)
}
readr::read_csv(here::here("data/cv-risk.csv"))
git_ci(
c("data-raw/cv-risk.R", "data/cv-risk.csv"),
"Download data into data folder"
)
styler::style_dir()
gert::git_add(".")
gert::git_commit("Download and save data")