Skip to content

Commit c46f85e

Browse files
committed
Add section in functions.Rmd showing how to use fun in workflow
1 parent ef78e37 commit c46f85e

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

episodes/cache.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Episode summary: Show how to get at the objects that we built
3535

3636
## Where does the workflow happen?
3737

38-
So we just finished running our first workflow.
38+
So we just finished running our workflow.
3939
Now you probably want to look at its output.
4040
But, if we just call the name of the object (for example, `penguins_data`), we get an error.
4141
```{r}

episodes/functions.Rmd

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ if (interactive()) {
3939
source("files/lesson_functions.R")
4040
```
4141

42-
## Create a function
43-
44-
### About functions
42+
## About functions
4543

4644
Functions in R are something we are used to thinking of as something that comes from a package. You find, install and use specialized functions from packages to get your work done.
4745

@@ -58,10 +56,11 @@ Furthermore, `targets` makes extensive use of custom functions, so a basic under
5856
### Writing a function
5957

6058
There is not much difference between writing your own function and writing other code in R, you are still coding with R!
61-
Let's imagine we want to convert the millimeter measurements in the Penguins data to centimeters.
59+
Let's imagine we want to convert the millimeter measurements in the penguins data to centimeters.
6260

6361
```{r}
6462
#| label: targets-functions-problem
63+
#| message: FALSE
6564
library(palmerpenguins)
6665
library(tidyverse)
6766
@@ -96,23 +95,30 @@ For our mm to cm conversion the function would look like so:
9695
mm2cm <- function(x) {
9796
x / 10
9897
}
99-
# use it
98+
```
99+
100+
Our custom function will now transform any numerical input by dividing it by 10.
101+
102+
Let's try it out:
103+
104+
```{r}
105+
#| label: targets-functions-cm-use
100106
penguins |>
101107
mutate(
102108
bill_length_cm = mm2cm(bill_length_mm),
103109
bill_depth_cm = mm2cm(bill_depth_mm)
104110
)
105111
```
106112

107-
Our custom function will now transform any numerical input by dividing it by 10.
113+
Congratulations, you've created and used your first custom function!
108114

109115
### Make a function from existing code
110116

111117
Many times, we might already have a piece of code that we'd like to use to create a function.
112118
For instance, we've copy-pasted a section of code several times and realize that this piece of code is repetitive, so a function is in order.
113119
Or, you are converting your workflow to `targets`, and need to change your script into a series of functions that `targets` will call.
114120

115-
Recall the code snippet we had to clean our Penguins data:
121+
Recall the code snippet we had to clean our penguins data:
116122

117123
```{r}
118124
#| label: code-to-convert-to-function
@@ -142,6 +148,8 @@ clean_penguin_data <- function(penguins_data_raw) {
142148
}
143149
```
144150

151+
Add this function to `_targets.R` after the part where you load packages with `library()` and before the list at the end.
152+
145153
::::::::::::::::: callout
146154

147155
# RStudio function extraction
@@ -178,9 +186,50 @@ vecmean <- function(x) {
178186

179187
:::::::::::::::::::::::::::::::::::::
180188

181-
Congratulations, you've started a whole new journey into functions!
182-
This was a very brief introduction to functions, and you will likely need to get more help in learning about them.
183-
There is an episode in the R Novice lesson from Carpentries that is [all about functions](https://swcarpentry.github.io/r-novice-gapminder/10-functions.html) which you might want to read.
189+
## Using functions in the workflow
190+
191+
Now that we've defined our custom data cleaning function, we can put it to use in the workflow.
192+
193+
Can you see how this might be done?
194+
195+
We need to delete the corresponding code from the last `tar_target()` and replace it with a call to the new function.
196+
197+
Modify the workflow to look like this:
198+
199+
```{r}
200+
#| label = "targets-show-fun-add",
201+
#| eval = FALSE,
202+
#| code = readLines("files/plans/plan_1.R")[2:21]
203+
```
204+
205+
We should run the workflow again with `tar_make()` to make sure it is up-to-date:
206+
207+
```{r}
208+
#| label: targets-run-fun
209+
#| eval: true
210+
#| echo: [5]
211+
pushd(make_tempdir())
212+
write_example_plan("plan_0.R")
213+
tar_make(reporter = "silent")
214+
write_example_plan("plan_1.R")
215+
tar_make()
216+
popd()
217+
```
218+
219+
We will learn more soon about the messages that `targets()` prints out.
220+
221+
## Functions make it easier to reason about code
222+
223+
Notice that now the list of targets at the end is starting to look like a high-level summary of your analysis.
224+
225+
This is another advantage of using custom functions: **functions allows us to separate the details of each workflow step from the overall workflow**.
226+
227+
To understand the overall workflow, you don't need to know all of the details about how the data were cleaned; you just need to know that there was a cleaning step.
228+
On the other hand, if you do need to go back and delve into the specifics of the data cleaning, you only need to pay attention to what happens inside that function, and you can ignore the rest of the workflow.
229+
**This makes it easier to reason about the code**, and will lead to fewer bugs and ultimately save you time and mental energy.
230+
231+
Here we have only scratched the surface of functions, and you will likely need to get more help in learning about them.
232+
For more information, we recommend reading this episode in the R Novice lesson from Carpentries that is [all about functions](https://swcarpentry.github.io/r-novice-gapminder/10-functions.html).
184233

185234
::::::::::::::::::::::::::::::::::::: keypoints
186235

0 commit comments

Comments
 (0)