Good session, as a R beginner learned some interesting things about how to write functions
Settings for input parameters, how to include more general inputs, and the possible pitfalls e.g. non-vectorised elements, were helpful
Presentation was very clear and I was able to check up on an unclear area for me by checking out the slides afterwards. Very useful to see curly brackets & vectorize()
Welcome!
this session is an 🌶🌶 intermediate practical designed for those with some R experience
Session outline
why functions?
basic syntax
understanding how functions work
vectorised functions
the mysteries of the paired curly brackets {{}} and the dots ...
anonymous functions
a bunch of ways to call functions
Why functions?
most beginners write repetitious code
repetitious code is hard to maintain
functions give you an easy way of repeating chunks of code
Basic syntax
think of this as a way of repeating yourself
in time-honoured fashion…
hi_wrld<-function(){"hello world"}hi_wrld()
[1] "hello world"
Adding arguments
most of the time, you’ll want to add arguments to your function
add a variable name inside the round bracket of function
Error in dplyr::summarise(mtcars, average = round(mean(column))) :
ℹ In argument: `average = round(mean(column))`.
Caused by error:
! object 'hp' not found
object ‘hp’ not found
we get used to R (and particularly tidyverse) helping us with some sugar when selecting column by their names
mtcars$hp / mtcars |> select(hp)
effectively, we’re just able to specify hp like an object, and R figures out the scope etc for us
that misfires inside functions. R isn’t sure where to look for an object called hp
In some cases, you might find yourself creating a function that’s only going to be used in a single location. In that case, it’s possible to define a nameless (anonymous) function, which is concise-but-nasty. For example:
(\(x)x*2)(5)
[1] 10
Here:
\(x) defines an anonymous function that takes one argument x.
x * 2 is the body of the function which determines how the function works
(5) is the value that we’re supplying to the function
Most usually, that anonymous function won’t be used with a single value, but within a pipe. Here, the supplied value is dropped, and substituted by the values passing along the pipe:
mtcars|>dplyr::select(wt)|>(\(x)x*2)()|># Double the weight of all the valuesdplyr::slice_max(wt, n =3)
wt
Lincoln Continental 10.848
Chrysler Imperial 10.690
Cadillac Fleetwood 10.500
Anonymous functions and the magrittr pipe
The magrittr pipe (%>%) offers a simple method of avoiding the horrid anonymous function syntax using the . placeholder: