forked from swcarpentry/r-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-best-practices-R.Rmd
97 lines (68 loc) · 6.08 KB
/
06-best-practices-R.Rmd
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
layout: page
title: Programming with R
subtitle: Best practices for writing R code
minutes: 30
---
```{r, include = FALSE}
source("tools/chunk-options.R")
```
> ## Learning Objectives {.objectives}
>
> Define best formating practices when writing code in R scripts;
> Synthesize a consistent personal coding style to increase code readability, consistency, and repeatability;
> Apply this style to one's own code
1. Start your code with an annotated description of what the code does when it is run:
```{r}
#This is code to replicate the analyses and figures from my 2014 Science paper.
#Code developed by Sarah Supp, Tracy Teal, and Jon Borelli
```
2. Next, load all of the packages that will be necessary to run your code (using `library`):
```{r, eval=FALSE}
library(ggplot2)
library(reshape)
library(vegan)
```
3. Set your working directory before `source()`ing a script, or start `R` inside your project folder:
One should exercise caution when using `setwd()`. Changing directories in a script file can limit reproducibility:
* `setwd()` will return an error if the directory to which you're trying to change doesn't exit or if the user doesn't have the correct permissions to access that directory. This becomes a problem when sharing scripts between users who have organized their directories differently.
* If/when your script terminates with an error, you might leave the user in a different directory than the one they started in, and if they then call the script again, this will cause further problems. If you must use `setwd()`, it is best to put it at the top of the script to avoid these problems.
The following error message indicates that R has failed to set the working directory you specified:
```
Error in setwd("~/path/to/working/directory") : cannot change working directory
```
It is best practice to have the user running the script begin in a consistent directory on their machine and then use relative file paths from that directory to access files (see below).
4. Annotate and mark your code using `#` or `#-` to set off sections of your code and to make finding specific parts of your code easier.
5. If you create only one or a few custom functions in your script, put them toward the top of your code so they are among the first objects created. If you have written many functions, put them all in their own .R file and then `source` those files. `source` will define all of these functions so that your code can make use of them as needed. For the reasons listed above, try to avoid using `setwd()` (or other functions that have side-effects in the user's workspace) in scripts you `source`.
```{r, eval=FALSE}
source("my_genius_fxns.R")
```
6. Use a consistent style within your code. For example, name all matrices something ending in `.mat`. Consistency makes code easier to read and problems easier to spot.
7. Keep your code in bite-sized chunks. If a single function or loop gets too long, consider looking for ways to break it into smaller pieces.
8. Don't repeat yourself--automate! If you are repeating the same code over and over, use a loop or a function to repeat that code for you. Needless repetition doesn't just waste time--it also increases the likelihood you'll make a costly mistake!
9. Keep all of your source files for a project in the same directory, then use relative paths as necessary to access them. For example, use
```{r, eval=FALSE}
dat <- read.csv(file = "files/dataset-2013-01.csv", header = TRUE)
```
rather than:
```{r, eval=FALSE}
dat <- read.csv(file = "/Users/Karthik/Documents/sannic-project/files/dataset-2013-01.csv", header = TRUE)
```
10. R can run into memory issues. It is a common problem to run out of memory after running R scripts for a long time. To inspect the objects in your current R environment, you can list the objects, search current packages, and remove objects that are currently not in use. A good practice when running long lines of computationally intensive code is to remove temporary objects after they have served their purpose. However, sometimes, R will not clean up unused memory for a while after you delete objects. You can force R to tidy up its memory by using `gc()`.
~~~{.r}
interim_object <- data.frame(rep(1:100,10),rep(101:200,10),rep(201:300,10)) # Sample dataset of 1000 rows
object.size(interim_object) # Reports the memory size allocated to the object
rm(interim_object) # Removes only the object itself and not necessarily the memory alloted to it
gc() # Force R to release memory it is no longer using
ls() # Lists all the objects in your current workspace
rm(list = ls()) # If you want to delete all the objects in the workspace and start with a clean slate
~~~
11. Don't save a session history (the default option in R, when it asks if you want an `RData` file). Instead, start in a clean environment so that older objects don't remain in your environment any longer than they need to. If that happens, it can lead to unexpected results.
12. Wherever possible, keep track of `sessionInfo()` somewhere in your project folder. Session information is invaluable because it captures all of the packages used in the current project. If a newer version of a package changes the way a function behaves, you can always go back and reinstall the version that worked (Note: At least on CRAN, all older versions of packages are permanently archived).
13. Collaborate. Grab a buddy and practice "code review". Review is used for preparing experiments and manuscripts; why not use it for code as well? Our code is also a major scientific achievement and the product of lots of hard work!
14. Develop your code using version control and frequent updates!
> ## Discussion - Best practice {.challenge}
>
> 1. What other suggestions do you have for coding best practices?
> 2. What are some specific ways we could restructure the code we worked on today to make it easier for a new user to read? Discuss with your neighbor.
> 3. Make two new R scripts called `inflammation.R` and `inflammation_fxns.R`. Copy and paste code into each script so that `inflammation.R` "does stuff" and `inflammation_fxns.R` holds all of your functions. __Hint__: you will need to add `source` to one of the files.