From bde3abd6eabbf8fc22029de1f5600108ceaeccd5 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Tue, 7 May 2024 14:33:10 +0200 Subject: [PATCH] docs(exercises): move template code above, plus move the "where are functions from?" to earlier. Closes #84 --- sessions/dplyr-joins.qmd | 44 ++++++++++++++++------------------------ sessions/functions.qmd | 8 ++++++++ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/sessions/dplyr-joins.qmd b/sessions/dplyr-joins.qmd index c818a51..5d25057 100644 --- a/sessions/dplyr-joins.qmd +++ b/sessions/dplyr-joins.qmd @@ -211,6 +211,24 @@ and extracts the user ID from it. **First step**: While in the `doc/learning.qmd` file, convert this code into a function, using the same process you've done previously. +Use this code as a guide to help complete the exercise tasks below: + +``` {.r filename="doc/learning.qmd"} +extract_user_id <- ___(___) { + ___ <- ___ |> + ___mutate( + user_id = ___str_extract(file_path_id, + "user_[0-9][0-9]?"), + .before = file_path_id + ) |> + ___select(-file_path_id) + return(___) +} + +# This tests that it works: +# extract_user_id(user_info_df) +``` + 1. Call the new function `extract_user_id` and add one argument called `imported_data`. - Remember to output the code into an object and `return()` it at @@ -232,32 +250,6 @@ same process you've done previously. 6. Add and commit the changes to the Git history with {{< var keybind.git >}}. -::: callout-tip -If you don't know what package a function comes from when you need to -append the package when using `::`, you can find out what the package is -by using the help documentation `?functionname` (can also be done by -pressing F1 when the cursor is over the function). The package name is -at the very top left corner, surrounded by `{ }`. -::: - -Use this code as a guide to help complete this exercise: - -``` {.r filename="doc/learning.qmd"} -extract_user_id <- ___(___) { - ___ <- ___ |> - ___mutate( - user_id = ___str_extract(file_path_id, - "user_[0-9][0-9]?"), - .before = file_path_id - ) |> - ___select(-file_path_id) - return(___) -} - -# This tests that it works: -# extract_user_id(user_info_df) -``` - ```{r solution-user-id-extract} #| eval: true #| output: false diff --git a/sessions/functions.qmd b/sessions/functions.qmd index 76520a1..f55db99 100644 --- a/sessions/functions.qmd +++ b/sessions/functions.qmd @@ -694,6 +694,14 @@ state which package the function is from, R will use the function that it finds first - which isn't always the function you meant to use. We also do this step at the end of making the function because doing it while we create it can be quite tedious. + +::: callout-tip +If you don't know what package a function comes from when you need to +append the package when using `::`, you can find out what the package is +by using the help documentation `?functionname` (can also be done by +pressing F1 when the cursor is over the function). The package name is +at the very top left corner, surrounded by `{ }`. +::: ::: Alright, let's go into `R/functions.R` and add `readr::` to each of the