Skip to content

Commit

Permalink
docs(exercises): move template code above, plus move the "where are f…
Browse files Browse the repository at this point in the history
…unctions from?" to earlier.

Closes #84
  • Loading branch information
lwjohnst86 committed May 7, 2024
1 parent 2604e78 commit bde3abd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
44 changes: 18 additions & 26 deletions sessions/dplyr-joins.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions sessions/functions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bde3abd

Please sign in to comment.