Want to help out or contribute?

If you find any typos, errors, or places where the text may be improved, please let us know by providing feedback either in the feedback survey (given during class) or by using GitHub.

On GitHub open an issue or submit a pull request by clicking the " Edit this page" link at the side of this page.

Appendix C — Extra exercises

C.1 Creating automatic analysis pipelines

C.1.1 Exercise: Update function to also calculate median and IQR

Time: ~8 minutes.

Let’s make a change to our function and test out how the tar_outdated() and tar_visnetwork() work.

  1. Open up the R/functions.R file.
  2. Add median and interquartile range (IQR) to the summarise() function, by adding it to the end of list(mean = mean, sd = sd), after the second sd. Note, IQR should look like iqr = IQR since we want the output columns to have a lowercase for the column names.
  3. Run tar_outdated() and tar_visnetwork() in the Console (or by using the Command Palette , then “targets outdated” or “targets visual”). What does it show?
  4. Style using the Palette (, then type “style file”). You might need to force a reformat if the code is too long by highlighting the line and using , then “reformat”.
  5. Run tar_make() in the Console or with the Palette (, then type “targets run”). Re-check for outdated targets and visualize the network again.
  6. Open up the Git interface and commit the changes to the Git history with or with the Palette (, then type “commit”).
Click for a potential solution. Only click if you are struggling or are out of time.
#' Calculate descriptive statistics of each metabolite.
#'
#' @param data Lipidomics dataset.
#'
#' @return A data.frame/tibble.
#'
descriptive_stats <- function(data) {
  data %>%
    dplyr::group_by(metabolite) %>%
    dplyr::summarise(dplyr::across(value, list(
      mean = mean,
      sd = sd,
      median = median,
      iqr = IQR
    ))) %>%
    dplyr::mutate(dplyr::across(tidyselect::where(is.numeric), ~round(.x, digits = 1)))
}

C.2 A general approach to doing statistical analyses

C.2.1 Exercise: How would you define a linear regression with parsnip?

Time: ~10 minutes.

Using parsnip’s “Examples” vignette as well as the code we wrote for the logistic regression above as a template, write parsnip code that would define a simple (an engine of"lm") linear regression model. Begin by making a new Markdown header and code chunk at the bottom of the doc/learning.qmd file, like listed below:

## Exercises
### Linear regression model definition

```{r}

```

After writing the code, style the file using the Palette (, then type “style file”). We will eventually delete these exercise text in the Quarto file, but for now, commit the changes to the Git history with or with the Palette (, then type “commit”).

Click for the solution. Only click if you are struggling or are out of time.
linear_reg_specs <- linear_reg() %>%
  set_engine("lm")

C.3 Building a website with Quarto

C.3.1 Exercise: Look through the new website folder

Time: ~5 minutes.

In your main project folder, there should now be a folder called _site/. Look through the folder. What files are now created? Open up the index.html file in your browser while in RStudio by clicking the index.html file and then selecting the “Open in web browser”.

Recall how we described what a website is. Can you see how these files and folders are simply HTML files that the browser shows as website? In the browser URL, change the index.html ending link to doc/report.html. See how it changes to the file based on how it is structured in the folder?

C.3.2 Exercise: Make figures prettier

Time: ~25 minutes

Looking through the Cell Reference documentation, complete these tasks.

  • Add a fig-cap option to each of the figures.

  • For the distribution plot, set the code chunk option column to page to make it bigger.

Look through the ggplot2 documentation on theme_set() and theme() to update the look of all your figures. In the setup code chunk of the doc/learning.qmd file, make a new theme object and use theme_set() to apply that theme to all plots. Use this as a scaffold:

```{r setup}
# ... previous code
updated_theme <- theme(
  # ... make changes
)

# Or start with a template like theme_classic()

updated_theme <- theme_TEMPLATE() +
  theme(
    # ...
  )

theme_set(updated_theme)
```