You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/functions.Rmd
+59-10Lines changed: 59 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -39,9 +39,7 @@ if (interactive()) {
39
39
source("files/lesson_functions.R")
40
40
```
41
41
42
-
## Create a function
43
-
44
-
### About functions
42
+
## About functions
45
43
46
44
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.
47
45
@@ -58,10 +56,11 @@ Furthermore, `targets` makes extensive use of custom functions, so a basic under
58
56
### Writing a function
59
57
60
58
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.
62
60
63
61
```{r}
64
62
#| label: targets-functions-problem
63
+
#| message: FALSE
65
64
library(palmerpenguins)
66
65
library(tidyverse)
67
66
@@ -96,23 +95,30 @@ For our mm to cm conversion the function would look like so:
96
95
mm2cm <- function(x) {
97
96
x / 10
98
97
}
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
100
106
penguins |>
101
107
mutate(
102
108
bill_length_cm = mm2cm(bill_length_mm),
103
109
bill_depth_cm = mm2cm(bill_depth_mm)
104
110
)
105
111
```
106
112
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!
108
114
109
115
### Make a function from existing code
110
116
111
117
Many times, we might already have a piece of code that we'd like to use to create a function.
112
118
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.
113
119
Or, you are converting your workflow to `targets`, and need to change your script into a series of functions that `targets` will call.
114
120
115
-
Recall the code snippet we had to clean our Penguins data:
121
+
Recall the code snippet we had to clean our penguins data:
Add this function to `_targets.R` after the part where you load packages with `library()` and before the list at the end.
152
+
145
153
::::::::::::::::: callout
146
154
147
155
# RStudio function extraction
@@ -178,9 +186,50 @@ vecmean <- function(x) {
178
186
179
187
:::::::::::::::::::::::::::::::::::::
180
188
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).
0 commit comments