3MW (How to write better and more powerful functions)

Guten Tag!

Many greetings from Ulm, Germany.

Powerful functions are your gateway to a happier coding experience. If done right, refactoring your code into separate functions can save you time and nerves.

Today, I'm going to show you two advanced concepts so that your functions can become even better. Specifically, we'll

  1. use arbitrary variables with curly-curly

  2. pass along variables with dot-dot-dot

Use arbitrary variables with curly-curly

Imagine that you want to count variables in your data set. For example, you could take the Palmerpenguins data set and count occurrences for each categorical variable. While you're at it, why not also compute relative counts as well? Here's how that might look.

Notice how the logic is always the same? The only thing that changes is the variable that we use. So, let's try to pour that logic into a new function. But if we're not careful, things may go awry. Have a look.

Damn, that's annoying. The straight-forward approach of just passing a variable to count() did not work. In this scenario, count() was looking for variables named "variable" instead of "island".

To fix that, you can use the curly-curly operator. It works by wrapping the variable into two-curly brackets (each side). By the power of curly-curly wizardry everything runs smoothly now.

Pass along variables with dot-dot-dot

Now, let's take it up a notch. Let's not count one but multiple variables. Super exciting, right? Here's an example that counts sex and species.

We cannot rewrite this into a function using curly-curly. It's designed for one variable only. But we can use dot-dot-dot instead.

Marvelous! Basically, dot-dot-dot is R's way of saying "We should take these arguments and push it somewhere else." As you can see, you need to do two things

  1. Add "..." to the function call

  2. Put "..." at the location where all the arguments are supposed to go

Recommended Resources

More on curly-curly and dot-dot-dot: For a more thorough explanation and a somewhat more complex example, check out my blog post "Writing versatile functions with R". Alternatively, you can also check out the corresponding YT video.

R4DS by Hadley Wickham: It's the R data science bible. So you probably know it. Still, it can't hurt to revisit the functions chapter of the book.

Best practices for functions by Cosima Meyer: I know that I have shared this two weeks ago. But it fits quite well here. So let me repeat this resource for our newcomers.  Cosima (under the guise of the R-Ladies account) shared a great list of tips for creating functions. Definitely worth a read.

That's a wrap! As always, I'm happy to hear your feedback about this week's issue. Feel free to write me on Twitter or reply to this mail.

Enjoy the rest of your day!Albert

Reply

or to participate.