-
Notifications
You must be signed in to change notification settings - Fork 0
/
w1_getting_started.Rmd
249 lines (155 loc) · 7.25 KB
/
w1_getting_started.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
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
---
title: "Week 1 - Getting Started"
author: "Nicholas Good ([email protected])"
output:
rmarkdown::html_document:
toc: true
toc_float: true
theme: yeti
---
```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, warning=FALSE, message=FALSE, results = 'hide')
```
---
# Course Materials
Course material will be made available via [GitHub](https://github.com/nickgood/r_for_fire_data). The classes will be largely example driven. We'll cover some areas in more detail than others, frequently returning to topics as we progress.
```{r}
# Navigate to the course Github page and view this document.
```
---
# Installing R
R is available from [CRAN](https://cran.r-project.org). There are distributions for multiple platforms (Linux, OS X and Windows). CRAN (The Comprehensive R Archive Network) is a network of servers from which the lastest versions of base R, R packages, and R documentation can be downloaded. Choose a CRAN mirror close to your current location.
```{r}
# Install the precompiled R distribution for your platform.
```
---
# Installing RStudio
Install RStudio from [rstudio.com](https://www.rstudio.com/products/rstudio/download/). RStudio is your interface with R. Download the free version of *RStudio Desktop*.
```{r}
# Install RStudio.
```
---
# Navigating RStudio
We'll start by taking a brief tour of RStudio and get a feel for some basic R tasks. RStudio has a default four pane window layout.
The ***source*** pane allows you to open files for editing. RStudio have developed a number of file types that you can use to document and present your work.
![](images/pane_source.PNG)
<br>
The ***console*** pane is where you can pass commands to R.
![](images/pane_console.PNG)
<br>
```{r, eval = FALSE}
# Let's try out some commands. One at a time, type each of the following commands into the console and press return:
seq(1, 10, 0.5)
round(pi, 1)
Sys.time()
?list.files()
plot(cars)
# Does R care if you use upper or lower case characters?
# What does prefixing a command with the ? symbol do?
```
The ***environment*** pane allows you to view R objects et cetera in your working environment. Different object types are grouped together in the ***environment*** panel. There is also ***history*** tab in this panel, where you can view commands you have run.
![](images/pane_environment.PNG)
<br>
At present your ***environment*** pane will be empty. We'll add some items to the environment in the next section.
The final panel contains the ***files***, ***plots***, ***packages***, ***help***, and ***viewer*** tabs. The ***files*** tab allows you to browse your local system directory. Graphics can be viewed in the ***plots*** tab. Package information is displayed in the ***packages*** tab. You can use the ***help*** tab to search for function information. Documents generated by RStudio can be viewed in the ***viewer*** tab.
![](images/pane_file.PNG)
<br>
Let's take a look at the different tabs:
```{r}
# Select the files, plots, packages, help, and viewer tabs in turn.
# What do you see in each tab?
```
RStudio settings can be customized via the ***preferences*** menu item.
![](images/preferences.PNG)
<br>
---
# Working directory
The working directory is the folder that works in unless you tell R otherwise. The `getwd()` command returns your current working directory. The `setwd(...)` command allows you to set the working directory.
```{r}
# Run the getwd() command in the console
# Where is your current working directory?
```
# RStudio projects
When working with R you'll very soon want to create a way to organize your work. RStudio facilitates organizing your work using `projects`. Let's create our first RStudio project. Select `file` -> `new project` -> `new directory` -> `empty project`, select a directory and name for your project.
```{r}
# Run getwd() again. What does it return?
# Run setwd(..)
# Set the working directory back to your project directory
```
# Saving your work
There are multiple ways to save your work in R/RStudio. There are basic R scripts (`.R files`) and more elaborate markdown (`.Rmd`) documents for example
Let's create a file to save our work in. We'll create an `R Notebook` file, a special type of file that can be used to document our work in an easy to read and reproducible format.
```{r}
#1. Open a new R Notebook: file -> new file -> R Notebook
#2. Save the notebook file and rename it.
#3. Preview the file by pressing the preview button.
#4. Edit and resave the file (e.g. give it a new title e.g. "week one")
#5. Run the code chunk using the green arrow.
#6. Preview the file again.
#7. Remove the example text.
#8. You can use this file for the rest of today's class.
```
# Libraries
R comprises a *base* version that we installed earlier. However, many of the tools we will use are will be from add-on *packages*.
To download a new package you can navigate to `tools` -> `install packages`. Or you can install via the command line:
```{r, eval = FALSE}
install.packages("tidyverse")
```
Once a package is installed you need to tell R to make it available for use. We can do this with the `library()` function:
```{r}
library(tidyverse)
```
You can use the RStudio menu (`tools` -> `check for package updates`) to update to the lastest stable package releases.
---
---
# R objects and assignment
R is [object oriented](http://adv-r.had.co.nz/OO-essentials.html#base-types). To create an object use the assignment operator `<-`. The `<-` operator is recommended over `=`, which also works (most of the time).
```{r, eval = FALSE}
# One at a time, type each of the following commands into the console and press return.
my_dataframe <- data_frame(x = seq(1, 20, 1), y = rnorm(20))
my_lm <- lm(y ~ x, my_dataframe)
# Take a look in your environment window, what do you see?
```
* R objects have a class associated with them. Different methods (functions) can be applied to objects of different classes. You can use the `class()` function to determine an object's class.
```{r, eval = FALSE}
# Use the `class()` function to determine the class of each object in your working environment.
```
* Class specific results can be produced when a method is applied to an object of a given class
```{r, eval = FALSE}
# Run the following code one line at a time:
summary(my_dataframe)
plot(my_dataframe)
summary(my_lm)
plot(my_lm)
```
* Note: R can get a little tricky when [evaluating](http://adv-r.had.co.nz/Computing-on-the-language.html) objects. For example when `""` are required around inputs can take a little time to get your head around
---
# Names
R is fairly liberal in terms of object names it will allow. For your sanity its worth imposing some rules of your own:
* use lower case characters.
* separate words with `_`.
* never start a name with a number.
* use `a-z`, `0-9` and `_` only.
---
# Comments
Use `#` to comment your code.
```{r, results = 'markup'}
# this is a comment
# comments are not evaluated
print(8 * 10) # comments can be placed after code on the same line
# use comments to concisely document your code
```
---
# Help!
To display text for a function type a `?` before the function
```{r}
?help
```
View the help documentation for the following functions:
```{r, eval = FALSE}
mean()
lm()
data_frame()
sort()
```
---