-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module_01_Intro_to_R.Rmd
202 lines (122 loc) · 6.96 KB
/
Module_01_Intro_to_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
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
---
title: 'Module 1: Introduction to R'
author: "Brian P Steves"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```
Module 1: Introduction to R
====================
## 1 Installing R
R is open source, free, and installs on most computing platforms including Linux, Windows and MacOS. You should be able to find a compatible download of the most recent version of R at the follwing URL.
http://www.r-project.org/
Follow the instructions provided for you operating system.
## 2 Installing RStudio IDE (inegrated development environment)
RStudio an integrated development environment for R meaning it provides a suite of tools to help you interact with R.
It contains a code editor with sntax highlighting, code completion and smart indentation. It also provides a well thought out layout for you to access your command history, current list of objects, plots, help, files, packages, etc.. from within on application.
There are even some more advanced features concerning report generation and version control integration which we will cover towards the end of class.
RStudio is also free, available for Windows, MacOS, and linux and can be downloaded for installation from the following URL.
http://www.rstudio.com/
Follow the instructions provided for you operating system.
## 3 Basics of R
### 3.1 Interactive R on the Console
In other words, entering code line by line rather than from a script.
#### 3.1.1 Starting and stopping R
To start R from the command line simply type "R" and you'll get something like the following.
```{}
R version 3.0.3 (2014-03-06) -- "Warm Puppy"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
```
In RStudio, this console has its own window called "Console" and should start up automatically when you start RStudio.
To quit R use the following command at the console prompt
```{r echo=TRUE, eval=FALSE}
q()
```
of if you quit RStudio it will quit you out of R.
#### 3.1.2 Getting help in R
You can get help on a specific topic using the "help()" function. An alternative shortcut to help is "?"
```{r eval=FALSE}
help("lm")
```
While using "?" is usually easier, there are time when the full help function is better. For example "?if" will not evaluate while "help('if')" returns a help file about "Control Flow" which is probably what you want.
You can also search the help files using "help.search()" function when you don't know the exact command you want help on. The short cut to this is to use "??".
```{r eval=FALSE}
help.search("lm")
```
To run examples from a paticular function...
```{r eval=FALSE}
example("lm")
```
#### 3.1.3 Assignment of data structures to objects
To can assign a data structure to an object using "<-" or "->" or the "assign()" function. The following are all equivalent.
```{r}
x<-c(10,7,4,5)
c(10,7,4,5) -> x
assign("x", c(10,7,4,5))
```
Calling the object by name with print it
```{r}
x
```
If you don't assign a result to an object the resulting value is printed and lost.
```{r}
c(10,7,4,5)
```
#### 3.1.4 Listing of Objects
All of the current ojects in memory can be listed using the "ls()" or "objects()" functions.
```{r}
ls()
```
#### 3.1.5 Recalling previous commands
While at the console, the up and down arrows will cycle the command prompt with the previously executed commands.
### 3.2 R Script files
Entering lines of code into the console might be fun but we can combine a series of commands into an R script file. These files are simply text files that end with the ".r" or ".R" extension and contain a series of R commands.
Once an R script is written and saved it can be evaluated in R using the "source()" command.
If you plan on running more than a couple lines of code it really helps to use script files as it is much easier to edit a script file and rerun it than it is to retype a long series of commands over again.
```{r eval=FALSE}
source("myScript.R")
```
## 4 Contributed Packages
One of the great things about R is the diversity of code that has been written and is freely available to everyone. The most common way people share R code is as a package of functions, usually with a unifing theme.
### 4.1 Finding Packages
#### 4.1.1 CRAN Task Views
Finding an appropriate package can be a bit of hit or miss as the quality ranges from exceptional to awful. One good place to start are the "Task Views" maintained on the main R project server.
http://cran.r-project.org/web/views/
For example, if you are interested in "Reproducible Research" there is a Task View for that...
http://cran.r-project.org/web/views/ReproducibleResearch.html
This task view is an annotated list of packages that relate to reproducible research.
Another important task view for ecologists and environmental scientists is the "Analysis of Ecological and Environmental Data" task view...
http://cran.r-project.org/web/views/Environmetrics.html
#### 4.1.2 Searching Google
If you already know which statistical technique you are looking to employ but don't know which R package might need you can always search Google for it. Unfortunately, the letter "R" isn't much of a unique name for a Google search on R code. However, if you change "R" to "CRAN R" in that query to refer to the "Comprehensive R Archive Network" you will often get better results.
### 4.2 Installing Packages
Once you know which R package you want to install you use the "install.packages()" command to install it.
```{r}
install.packages("vegan", repos='http://cran.us.r-project.org')
```
If you haven't told R or RStudio where you want to download this package from, it may ask you for you to select a mirror. You'll probably want to pick either the closest mirror server or one that you feel is the quickest. Once the package is downloaded, R will attempt to install it on your computer. If anything goes wrong in the installation an error will be reported. Packages can be installed for all users on the computer or just one specific user. If you don't have full admin rights on your computer your package will be installed in a local user library of packages.
### 4.3 Loading Packages
To load an install package for use you use the "library()" function.
```{r}
library(vegan)
```
## Homework
1. Install the latest versions of R onto your computer.
2. Install the latest version of RStudio onto your computer.
3. Find and install at least one R package.