forked from anabento/R_Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
M3_Script.Rmd
165 lines (107 loc) · 4.05 KB
/
M3_Script.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
title: ""
author: ""
date: ""
output: html_document
---
#<span style="color:cadetblue">Writing a script</span>
***
This module will cover:
- script best practices
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#<span style="color:cadetblue">The importance of a script</span>
>1. Permanent
>2. Repeatable
>3. Annotated
>4. Shareable
###<span style="color:orangered">Practice:</span> We will do this exercise together...
##<span style="color:cadetblue">Step 1</span>
```
File --> New File --> R Script or Top left green and white plus sign drop down then select R Script
```
##<span style="color:cadetblue">Step 2</span>
```
The # symbols declares that anything after it is a comment
You should start with some information at the top, for example:
#############################
## Author: ###
## Date: ###
## Purpose: ###
#############################
```
##<span style="color:cadetblue">Step 3</span>
Leave an empty "blank line" - These are important to create chunks of code and annotation
##<span style="color:cadetblue">Step 4</span>
Next <span style="color:orangered">**if**</span> your script has code that is standalone and independent, we recomend you always start a script by clearing R's brain
``` rm(list=ls())```
<span style="color:orangered">**If**</span> your script is a function or group of functions to be sourced by another script, skip this step!!
Try typing the ```rm``` function instead of copy pasting it. You will notice Rstudio **closes brackets** for you automatically
##<span style="color:cadetblue">Step 5</span>
Leave another empty "blank line"
##<span style="color:cadetblue">Step 6</span>
###Now you are ready to start writing your first script! Copy paste the code inside the ```[] ``` Then move to <span style="color:cadetblue">Step 7</span>
We will go over how to construct these functions in the next module
A loop
```{r loop}
for(i in 1:10){ # for loop run from 1 to 10
print("Darwin rules!") # write a sentence 10 times
print(i*i) # also print each number multiplied by itself
}
```
<span style="color:orangered">Practice:</span> annotate the function
```{r function}
Isit <- function(x){
if (x == 10)
print("It's a ten!")
else print("Not a ten!")
}
Isit(10)
Isit(1)
```
A function and a plot
```{r plot}
# A simple function that needs functions from the ape library to run
# ape is a library of functions for analysis of evolutionary relationships:
Tree <- function(x){
#this function creates a plot of a random evolutionary tree
#of size x
require(ape)
tree <- rcoal(x)
plot(tree)
}
Tree(30)
```
<span style="color:orangered">**_Check_**:</span> Notice the colours in your script, this is syntax highlighting. Separates:
>1. comments
>2. R functions
>3. numbers
>4. other things
##<span style="color:cadetblue">Step 7</span>
Once you are done writing your script and you are happy with the commenting. <span style="color:orangered">**Save**</span> it in your <span style="color:orangered">**working directory**</span> of your project. Give it a meaningful name.
##<span style="color:cadetblue">Step 8</span>
Open your script and run it!
```
File --> Open file --> Search for your working directory then run all or parts of it
```
<span style="color:orangered">**_Or_**</span> you can double click on your R script open it in R studio and run specific parts of it
```
Mac: cmd+return
Windows: ctrl+enter
or pressing Run
```
<span style="color:orangered">**_Or_**</span>
```
setwd("your path") # sets your working directory. You can also do this mannually
source("Script1.R") # source your script, rename accordingly
```
##<span style="color:cadetblue">Step 9</span>
It's a good idea to create a README text file that describes your project and keep it in the project
folder.
This file should describe the overall project and the files in the project folder
###<span style="color:orangered">Practice:</span> Create a README text file
```
File --> New File --> Text file
Save it using the name README.txt" in the same directory of your project
```