A script using functions
Here is another script for analysing the Patheria dataset, this time written with functions (see the messy version here). As before, we want to download the Pantheria dataset, and make some plots of species-level life history variables, highlighting different orders in the plots.
In contrast to the messy script, this one sources a function “script-fun.R”, which contains a bunch of useful functions for plotting the data. These functions are included at the bottom of this page. As a result, our analysis script
- Is much shorter and more readable than the messy code.
- Is less likely to contain bugs, because it maintains a clean workspace, and each bit of code is only written once
- Does a lot more, with just a few more lines of code. By combining the functions we can make many more plots, much faster.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
The functions file, loaded using source
in the main script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
Download these files: main script and the function definitions.