-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR_basics_2.Rpres
345 lines (296 loc) · 10 KB
/
R_basics_2.Rpres
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<style>
.small-code pre code {
font-size: 1em;
}
</style>
R basics 2 - Automating analyses
========================================================
author: Kevin Shook
date:
autosize: true
Automating your work
========================================================
- Using scripts, you can do more work in less time
- Code can be re-used, making you more productive
Organizing your R code
========================================================
![](Harder_R_lunch_talk.png)
Projects
========================================================
- It's a good idea to create a new **R** project for each new work project
- Can go in the code folder
- R studio command is ```File|New project```
- Creates a file with the extension ```.RProj``
- Contains all of the project settings
- Creates a ```.Rhistory``` file
- Contains all of the commands you execute
- Creates a ```.RData``` file
- Contains all of the variables in the session
Projects cont'd
========================================================
- Projects allow the use of **git** to manage versions of files
- Also allow you to set options for just this project
- Once you have created a project file, just double-click to load it and run RStudio
- Will remember all of your project settings
R script files
========================================================
- Text files with the extension .R
- Can be run in several ways
- All at once
- One line at a time
Loading R command files
========================================================
- Typing ```source(filename)``` loads in a .R file and runs all commands
- Using the menu ```File|OpenFile...``` loads in .R file **without** automatically running the commands
Example #1
========================================================
- Using ```File|Open File```, load in file ```Example1.R```
- Press **[Ctrl][Enter]** to step through the code one line at a time
- Now check ```Source on Save```
<div align="left">
<img src="source_save.png" width=400>
</div>
- click on the save icon
- What happened?
Running all lines
========================================================
- When you execute *all* of the lines, they are not echoed to the screen
- If you want to output a value, you have to tell **R** to do it
- Use the command ```cat()``` to print things out
- All of the values will be printed on one line
- Have to add line breaks using the symbol ```"\n"```
Editing the file
========================================================
class: small-code
- Make these changes to lines **8** and **9**:
Change the lines
``` {r, eval = FALSE}
actual_mean
actual_sd
```
to
```{r, eval=FALSE}
cat("actual mean:", actual_mean, "\n")
cat("actual sd:", actual_sd, "\n")
```
- Save, and re-run
- Congratulations! You have now built an **R** program
Editing R script files
========================================================
- R Studio has a very good built-in editor
- You can change the appearance (colours, fonts, other settings) using the menu
```Tools|Global Options```
Programming in R
========================================================
- **R** scripts are really computer programs
- Can include all of the elements of other programs
- data input and output
- branches
- loops
- functions
Functions
========================================================
- Writing your scripts as functions makes them more repeatable
- Can build your own library of functions
- functions are not executed automatically when the file is sourced
```{r eval=FALSE}
funcName <- function(parameters) {
}
```
Parameters
========================================================
- All function variables are separate from the rest of your **R** code
- Makes functions secure and repeatable
- You pass values into the function using parameters
- Parameters can have default values
Returning values
========================================================
- A function can only return a single value
- Can be a scalar, vector, data frame etc.
- To return more than 1 value, put them in a list
- By default the last variable is returned
- sloppy and potentially dangerous
- use ```return()`` instead
Function example
========================================================
```{r}
cv <- function(values) {
coeff_of_var <- sd(values) / mean(values)
return(coeff_of_var)
}
```
***
```{r}
x <- runif(5) # random numbers
x
cv(x)
```
Loading a function file
========================================================
class: small-code
```{r}
source('Example2.R')
saturatedVP
```
***
```{r}
saturatedVP(20)
```
When to write a function?
========================================================
- If the code will be used several times
- especially if it will be used with different inputs
- or it will be used by someone else
- If the logic is very complex
- If it calls many other functions
- If the order of operation is complex
- This type of code needs a debugger
Debugger
========================================================
- R has a built-in debugger
- Allows you to step through a function, examining the values of variables
- Set break-points by clicking on a line
- Save the file
- Run the function, passing the parameters
- Will run to that line
- Variable values are listed in the Environment pane
- Step through the function with **[F10]**
Debugging
========================================================
![](debugger.png)
Complex statements
========================================================
- These statements are usually used in **R** *programs*
- Control the order of execution of code
- Especially useful in functions
If statements
========================================================
- need a condition which evaluates to be ```TRUE``` or ```FALSE```
- any number of lines can be between the braces
- good style to indent
```{r}
a <- 2
if (a %%2 == 0) {
cat("even")
} else {
cat("odd")
}
```
Loops
========================================================
- **R** is interpreted, meaning that each line is converted to machine-language and then executed
- Much slower than compiled languages (C or Fortran)
- Loops are generally a bad idea, as they are very slow in **R**
- Can often be avoided, but sometimes you have to use them
For loops
========================================================
- Used when loop will execute a fixed number of times
```{r}
for (i in 1:5) {
cat(i, " ")
}
```
Avoiding loops
========================================================
- "If you're using a loop, you've failed"
-------------------------------------------
- **R** is written in C and Fortran
- Any function which loops *automatically* is much faster than doing it yourself
- Most functions can be applied automatically to all of the rows or columns in a data frame or matrix
- Some functions also do the types of things loops are often used for
ifelse
========================================================
```{r, eval=FALSE}
ifelse(test, true_val, false_val)
```
- applies test to each element in a variable and returns either the true or false value
cumsum() and diff()
========================================================
class: small-code
```{r}
x
cumulative <- cumsum(x)
cumulative
diff(cumulative)
```
Apply functions
========================================================
- Some functions cannot use vectors or data frames
- For these, use the ```apply``` series of functions to loop over your values
- Much faster as looping is compiled
- ```sapply``` (simple apply) is the easiest to use
Example #2 - sapply() vs loop
========================================================
```{r}
load("R_basics_2.RData")
head(Saskatoon, 3)
rows <- nrow(Saskatoon)
rows
```
Using a loop
========================================================
```{r, eval=FALSE}
system.time({for (i in 1:rows)
Saskatoon$satVP[i] <- saturatedVP(Saskatoon$t.1[i])})
```
```
user system elapsed
287.476 56.872 344.366
```
Using sapply()
========================================================
```{r}
system.time({Saskatoon$satVP <- sapply(Saskatoon$t.1, saturatedVP)})
```
Mixed language programming
========================================================
- You can call code written in C, or Fortran from R
- Use the compiled code for speed
- Need to have a compiler
```{r, eval=FALSE}
meanepsilon2d <- function(x, r, q){
xsize <- as.integer(nrow(x))
meanepsilon <- 0
r <- as.integer(r)
retdata <- .Fortran("meanepsilon2d", x, xsize, r, q, meanepsilon, PACKAGE = 'multifRactal')
return(retdata[[5]])
}
```
A useful loop
========================================================
class: small-code
- This will loop though all of the files in the directory whose names fit the specifications
- I have added it to my copy of **RStudio** as a snippet
- Overhead of looping is small compared to reading/processing data
```{r, eval = FALSE}
filespec <- '*.csv' # using wildcards
FilePattern <- glob2rx(filespec) # wild cards to regular expression
FileList <- list.files(pattern = FilePattern)
NumFiles <- length(FileList) # get number of file names
for (i in 1:NumFiles){ # loop through all file names
filename <- FileList[i]
...
}
```
Summary
========================================================
- The real power of **R** comes from writing your own code
- Eventually you will build up a library of frequenty-used functions
- We will be seeing how to combine your code with outputs, graphs and images
- produces very reproducible research
Questions/answers
========================================================
- Bulk downloading data from EC?
- check out `downloadMSCobs` in package `MSCr`
- Several box-plots in one plot?
- check out `facet_grid` or `facet_wrap` in package `ggplot2`
- Navigating a directory tree?
- check out `list.files(recursive=TRUE)`
Questions/answers cont'd
========================================================
- Huge data structures?
- check out https://www.r-bloggers.com/five-ways-to-handle-big-data-in-r/
- GUI for data QA/QC
- can build web apps using package `shiny`
- **R** is not the right program for complex GUI applications
- GUI applications in other languages can call **R** code