+ - 0:00:00
Notes for current slide
Notes for next slide

Programming with data

Writing functions

Ben Baumer

SDS 192
Oct 19, 2020
(http://beanumber.github.io/sds192/lectures/mdsr_scale_01-functions.html)

1 / 11

Functions

Do you ever find yourself with .Rmd files that look like this?

my_df1 %>%
...
# Do some stuff to my_df1
...
my_df2 %>%
...
# Do the same stuff to my_df2
...
my_df3 %>%
...
# And again to my_df3
...
2 / 11

For example

What if I want to draw the same kind of plot several times?

my_df1 %>%
ggplot(aes(x = var1, y = var2, color = var3)) +
geom_point() +
geom_line()
my_df2 %>%
ggplot(aes(x = varA, y = varB, color = varC)) +
geom_point() +
geom_line()
my_df3 %>%
ggplot(aes(x = var1A, y = var2B, color = var3C)) +
geom_point() +
geom_line()
3 / 11
4 / 11

User-defined functions

name_of_function <- function(data, var = "value") {
...
...
<valid R code>
...
...
return(x)
}
  • inputs:

    • the arguments: data, var
    • data is required
    • var is optional -- has a default value of "value"
  • output:

    • the return value
    • by default output of last line in function
    • here, explicitly the object x
5 / 11

Scoping: global variables OK

my_cars <- function(mod, n = 6) {
mpg %>%
filter(model == mod) %>%
head(n)
}
my_cars("civic")
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 honda civic 1.6 1999 4 manual(… f 28 33 r subcomp…
## 2 honda civic 1.6 1999 4 auto(l4) f 24 32 r subcomp…
## 3 honda civic 1.6 1999 4 manual(… f 25 32 r subcomp…
## 4 honda civic 1.6 1999 4 manual(… f 23 29 p subcomp…
## 5 honda civic 1.6 1999 4 auto(l4) f 24 32 r subcomp…
## 6 honda civic 1.8 2008 4 manual(… f 26 34 r subcomp…
6 / 11

Default values

my_cars <- function(mod = "civic", n = 6) {
mpg %>%
filter(model == mod) %>%
head(n)
}
my_cars()
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 honda civic 1.6 1999 4 manual(… f 28 33 r subcomp…
## 2 honda civic 1.6 1999 4 auto(l4) f 24 32 r subcomp…
## 3 honda civic 1.6 1999 4 manual(… f 25 32 r subcomp…
## 4 honda civic 1.6 1999 4 manual(… f 23 29 p subcomp…
## 5 honda civic 1.6 1999 4 auto(l4) f 24 32 r subcomp…
## 6 honda civic 1.8 2008 4 manual(… f 26 34 r subcomp…
7 / 11

Default values overridden

my_cars("jetta", n = 3)
## # A tibble: 3 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 volkswagen jetta 1.9 1999 4 manual(m5) f 33 44 d compa…
## 2 volkswagen jetta 2 1999 4 manual(m5) f 21 29 r compa…
## 3 volkswagen jetta 2 1999 4 auto(l4) f 19 26 r compa…
8 / 11

Naming arguments optional

my_cars("camry", 2)
## # A tibble: 2 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 toyota camry 2.2 1999 4 manual(m5) f 21 29 r midsi…
## 2 toyota camry 2.2 1999 4 auto(l4) f 21 27 r midsi…
my_cars(mod = "corolla", 2)
## # A tibble: 2 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 toyota corolla 1.8 1999 4 auto(l3) f 24 30 r compa…
## 2 toyota corolla 1.8 1999 4 auto(l4) f 24 33 r compa…
9 / 11

Order of arguments matter if unnamed

my_cars(2, "corolla")
## # A tibble: 0 x 11
## # … with 11 variables: manufacturer <chr>, model <chr>, displ <dbl>,
## # year <int>, cyl <int>, trans <chr>, drv <chr>, cty <int>, hwy <int>,
## # fl <chr>, class <chr>
my_cars(2, mod = "corolla")
## # A tibble: 2 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 toyota corolla 1.8 1999 4 auto(l3) f 24 30 r compa…
## 2 toyota corolla 1.8 1999 4 auto(l4) f 24 33 r compa…
my_cars(n = 2, "corolla")
## # A tibble: 2 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 toyota corolla 1.8 1999 4 auto(l3) f 24 30 r compa…
## 2 toyota corolla 1.8 1999 4 auto(l4) f 24 33 r compa…
10 / 11

Writing functions checklist

Pay attention to:

  • names of arguments

  • default argument values

  • local vs. global objects

  • return values

11 / 11

Functions

Do you ever find yourself with .Rmd files that look like this?

my_df1 %>%
...
# Do some stuff to my_df1
...
my_df2 %>%
...
# Do the same stuff to my_df2
...
my_df3 %>%
...
# And again to my_df3
...
2 / 11
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow