Neil Currie Profile picture
Jan 6, 2023 17 tweets 6 min read Read on X
Struggling with functions in R?

You're not alone!

In this thread, I'll show you some tips and tricks for upping your function-writing game.

Let's dive in...

#rstats #coding #datascience
Functions are blocks of code organised together which perform a specific task.

R comes with many built-in functions which we can add to by downloading fantastic packages.

However, writing your own functions is where the true power of R lies - though it can be confusing.

1/16
In this thread I will cover:

1. When to write functions
2. How to write functions
3. Tidy evaluation
4. Passing multiple arguments with ellipsis ...
5. Error handling arguments
6. Side effects

2/16
A question I hear from newer coders is when should I actually write a function?

I follow some basic rules of thumb to decide went to turn code into a function, though it isn't an exact science.

1. The rule of 3.
2. The rule of organisation
3. The rule of testing.

3/16
1. The rule of 3

If I repeat some code 3 or more times, or I expect to, then I write a function.

Many errors enter code from copy + paste or retyping.

By calling a function many times you reduce the chances of this error, and to update code you need only change one part.

4/16 # Bad  sample1 <- sample(c(...
2. The rule of organisation

Often code can become long and complex.

In these cases I will split code into functions where I think this will aid readability and manageability.

Often the inside of a big for loop is a good candidate.

5/16
3. The rule of testing

Sometimes code contains calculations or small chunks that are crucial to the program or are logically complex and I want to test them individually.

In these cases I write functions.

To learn more about unit testing have a look here:

6/16
Arguments are the variables which are passed into the function.

They can be matched positionally or by name.

You can create default values for your function using = in the function definition.

7/16 # If you cast your mind bac...
If you have used the tidyverse you will have come across tidy evaluation.

Tidy eval allows us to use code like select(data, x) instead of select(data, "x").

But how can we take advantage of that behaviour with our own functions?

8/16
To use tidy eval in our own code we use double curly brackets {{arg}} to wrap the argument.

If the argument relates to the left hand side (i.e. a name) of a tidyverse function like mutate we also need to use :=

9/16 dummy <- tibble(id = 1:20, ...# Will work with the curly ...
Arguments are often a great source of errors.

Checking for these errors and telling the user what they should do is great practice.

Some good checks of arguments are type, structure and validity of values supplied.

Be proportionate and try to anticipate likely errors.

10/16 calculate_c <- function (si...
The ellipsis allows functions to take any number of arguments.

Some good examples are the paste and print functions.

In our own functions they are especially handy when we write wrapper style functions.

11/16
When using ... in functions it is good practice to place it after your main arguments but before any additional arguments with default values.

This makes supplying the ellipsis arguments easier when using the function.

12/16 # Maybe you want to use som...
We return variables from functions using the return function or, if missing, R will return the last evaluated object.

But sometimes when we write functions we aren't interested in the return value but its side effects e.g. when creating a plot or outputting data.

13/16
In these cases it is good practice to return the first argument unmodified.

Wrap it in the invisible function so it doesn't clutter your console by printing.

You could easily put this in a pipeline now.

14/16 sample_and_write <- functio...
To recap:

There are several rules of thumb to decide when a function is needed.

Some tricks like tidy eval and ... make your functions more powerful.

ALWAYS write error handling.

The return value isn't always important. Sometimes the side effect is the main event.

15/16
Thanks for reading, if you liked this thread follow me @neilgcurrie for mainly R and data tweets.

Website:

shoogle.co

YouTube

youtube.com/@shoogle

Code:

github.com/neilcuz/thread…

16/16

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Neil Currie

Neil Currie Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @neilgcurrie

Dec 16, 2022
How to create dumbbell plots with ggplot2

Impress clients, make the complex simple and up your data vis game.

#rstats #datavis #datascience A dumbbell chart showing fl...
Dumbbell plots are a great way of communicating information that can be grouped then split into two points.

I used one for TidyTuesday earlier this year:

github.com/neilcuz/tidytu…

But making them can be a little tricky.

I can show you how step-by-step.
Dumbbell plots can be created using 2 ggplot2 building blocks: geom_point and geom_line.

So let's start building from the ground up with some examples in each before combining to make a dumbbell plot.
Read 16 tweets
Nov 17, 2022
How to map with purrr in R...

...so you can ditch the for loops, tidy your code and handle errors with ease.

#rstats #datascience #coding
The map family of functions are a powerful weapon in your coding toolkit.

Maybe you've used the apply family of functions before?

These are similar but the syntax is much more straightforward and consistent across the functions.

Let's dive in.

1/11
First, when would you use a map function?

Whenever you would use a for loop.

Let's look at an example.

One way to refactor this code is to replace everything inside the loop with a function.

2/11 inputs <- list(runif(10), runif(10), runif(10), runif(10)) omean_top_5 <- function (x) {      x_ordered <- sort(x, decre
Read 12 tweets
Oct 28, 2022
Ever struggled with error handling in R?

tryCatch can be a little tricky to learn.

But there's a simpler, lesser known alternative.

#rstats #coding #datascience
Errors in R can be annoying.

There's nothing worse than running a long piece of code then, part way through, it crashes.

You need to rewrite your code and start again.

That is where error handling comes in.

1/11
You might have heard of the purrr package.

It's part of the tidyverse and contains the amazing family of map functions.

But it's also got 2 other functions we can use for error handling that are much easier than tryCatch.

2/11
Read 12 tweets
Oct 20, 2022
How to use R with GitHub

...so you can share your projects, version control code and stand out from the crowd with a data portfolio

#rstats #coding #datascience
GitHub is an online platform used for hosting, tracking and collaborating on software projects, built on a free version control system, Git.

GitHub knowledge is a key skill for any data professional.

And a portfolio can make you seriously stand out from the crowd.

1/20
Getting setup can be a little tricky, but I can show you how.

The first thing to do is install git to your machine.

But you may already have it.

Run git --version in the shell to find out.

If it prints the version, congratulations! You already have it.

2/20
Read 21 tweets
Oct 7, 2022
Writing functions in R troubling you?

How and when to write your own functions (including the mysteries of non-standard evaluation and the ellipsis ...)

#rstats #coding #datascience
Functions are blocks of code organised together which perform a specific task.

R comes with many built-in functions which we can add to by downloading fantastic packages.

However, writing your own functions is where the true power of R lies - though it can be confusing.

1/16
In this thread I will cover:

1. When to write functions
2. How to write functions
3. Tidy evaluation
4. Passing multiple arguments with ellipsis ...
5. Error handling arguments
6. Side effects

2/16
Read 17 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(