Session details

  • Date of session: 01 Jun, 2018
  • Instructor: Luke W. Johnston
  • Session level: Intermediate
  • Programming language: R

Session content

This is the code used during the session. I’ve added some comments and more explanation to the code.

All actions in R are functions

The + is a function, mean() is a function, [] is a function… everything that does something is called a function in R. So this to add 1 with 1:

… is a function that takes 1 and adds 1 to it. Functions have the basic structure of:

  • the name of the new function add_nums <-
  • the function to create the new function function(), along with the arguments num1, num2
  • the code to do the action of the function, everything between {}
    • should have a return() at the bottom that says what the function creates

You can use the new function by running the above code and writing out your new function, with arguments to give it.

There are a few things to consider. In R there are different “methods” of functions. This is way above what is necessary for this session, but if you are curious this website has a great explanation of the different methods (e.g. S3 methods). Be warned, the website is fairly advanced!

You can always look at the contents of all functions in R. So an example of an S3 function:

Ok, let’s get to something a bit more interesting. Usually we create plots that are more or less the same each time, but with different variables or data. So this is a great example of using a function to simplify your code. Let’s load up the ggplot2 package for plotting and the gapminder dataset.

Let’s plot year by life expectancy:

What if we wanted to see another plot by pop over time:

Or another plot… and so on. This starts getting a bit tedious, as you are just copying and pasting. There is a better way! Convert it into a function! The typical process for converting code into a function is first to write the code and make sure it works. Then wrap it in a function. And start replacing the variable names with the arguments. So:

But, this function won’t work! That’s because there is a tricky bit that you will quickly encounter in R… And that is called non-standard evaluation (NSE; check out here or here for more indepth look at what non-standard evaluation is). Because ggplot2 uses NSE, you will have to do things slightly differently. The aes in ggplot2 uses NSE. So you have to use aes_string instead.

If you want to make sure that who ever uses your function will not use a wrong argument, you can use “defensive programming” via the stopifnot() function. This forces the code to only work if xvar and yvar are character (e.g. "this") argument.

This NSE evaluation also happens in a popular package called dplyr. If we wanted to do a common analysis or data wrangling like so:

… and do this again but change pop to another variable, we can create a function.

… then use dplyr’s standard evaluation functions, which are usually the function with a _at or _if or other variation of that at the end…

And we can use it:

Send the output to create a table!

Table caption here
continent year lifeExp
Africa 1952 39.13550
Africa 1957 41.26635
Africa 1962 43.31944
Africa 1967 45.33454
Africa 1972 47.45094
Africa 1977 49.58042

We can even use both our functions together!

Resources


This work is licensed under a Creative Commons Attribution 4.0 International License. See the licensing page for more details about copyright information.