-
Notifications
You must be signed in to change notification settings - Fork 5
/
120_installing_R.Rmd
307 lines (146 loc) · 10.1 KB
/
120_installing_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
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
<!-- This file by Martin Monkman and Julie Hawkins
is licensed data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAAWElEQVR42mNgGPTAxsZmJsVqQApgmGw1yApwKcQiT7phRBuCzzCSDSHGMKINIeDNmWQlA2IigKJwIssQkHdINgxfmBBtGDEBS3KCxBc7pMQgMYE5c/AXPwAwSX4lV3pTWwAAAABJRU5ErkJggg==under a Creative Commons Attribution 4.0 International License
https://creativecommons.org/licenses/by/4.0/
The tables of functions are modified from originals by Gina Reynolds, [Tidyverse in Action](https:/EvaMaeRey/tidyverse_in_action) -->
```{r setup-120, echo = FALSE, eval=FALSE}
library(knitr)
opts_chunk$set(message = FALSE, warning = FALSE, cache = TRUE)
options(width = 100, dplyr.width = 100)
```
# Setting up the R ecosystem {#install-R}
## Objectives
At the end of this section, you will have:
* Installed R
* Installed RStudio
* Installed your first packages
* Created an RStudio project for the course
## Installing R & RStudio
How do we install R and RStudio on our computers?
_It depends_
* operating system (Windows, Mac, or Linux)
* administrative privilege & network permissions
### References
[_R Cookbook, 2nd edition_, "Getting Started and Getting Help", https://rc2e.com/](https://rc2e.com/gettingstarted)
* [1.1 Downloading and Installing R](https://rc2e.com/gettingstarted#recipe-id001)
* [1.2 Installing RStudio](https://rc2e.com/gettingstarted#recipe-id002b)
* [3.10 Installing Packages from CRAN](https://rc2e.com/navigatingthesoftware#recipe-id012)
Danielle Navarro, [_running R on your own computer_](https://youtube.com/playlist?list=PLRPB0ZzEYegOZivdelOuEn-R-XUN-DOjd)
* A series of videos on youtube, with installation instructions for different operating systems (Mac, Windows, and Ubuntu linux), as well as loading and installing packages from both CRAN and GitHub (where many in-development packages are available)
## Step 1 - Installing R
* Open https://posit.co/download/rstudio-desktop/ in your browser.
* Click on Step 1: Install R's button "DOWNLOAD AND INSTALL R" which will take you to https://cran.rstudio.com/
> If you need to select a CRAN mirror site, choose one near you or the top one listed as "0-Cloud," which tends to work well for most locations (https://cloud.r-project.org/).
* Click on relevant "Download R for ..." link (e.g., Linux, macos, Windows)
- *Windows*: click on "base", then the link for the latest version of R (an .exe file)
- *Mac*: click on the link for the latest version of R (an .pkg file), under "Latest Release:"
* When the download completes, double-click on the .exe file (Windows) or the .pkg file (macOS) and answer the usual questions.
## Step 2 - Installing RStudio
* Open https://posit.co/download/rstudio-desktop/ in your browser.
* Click on Step 2: Install RStudio Desktop's button for downloading the latest version of RStudio.
* When the download completes, double-click on the .exe file (Windows) or the .pkg file (macOS) and answer the usual questions.
## Step 3 - Install first packages
Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages (~30) such as {base}, {datasets}[^1], {graphics}, {stats}, {utils} and many more. Hundreds may be downloaded and installed. Once installed, they have to be loaded into the session to be used.
[^1]:Note that the datasets package has over 100 datasets to use (e.g., {mtcars} and {iris}).
To see what packages are currently installed, click on the Packages Pane (near the Files Pane) on bottom right, or use `library()`.
```{r, eval=FALSE}
library()
```
### Installing new packages
There are hundreds more R packages than the standard packages that install with R.
Some of the packages we will use in this course are `tidyverse` (a special package of consistent packages that work together), `here` (simpler way to find files), `lubridate` (dealing with dates more easily), `readxl` (to read data from an Excel file), `janitor` (data frame cleaning and exploring), etc.
![](Images\02_install_package_how.png)
To install a package:
* click on the Packages Pane and then Install.
* Install from: Repository (CRAN)
* Packages: Type in the package name(s)
* Install to Library: C:/RPackages/library [Default]
* Install dependencies: checked
* click "Install"
Or, to install the package called {here}, use:
```
install.packages("here")
```
You can see that a package is installed by searching it in Packages pane search, or scrolling down.
![](Images\02_packages_search.png)
The reference page for the {here} package: https://here.r-lib.org/
### Package: `{tidyverse}`
For BIDA302, we will be working extensively with the packages that are part of the tidyverse. To make our lives easier, the core tidyverse packages are bundled together and can be installed in one step, with a package called `{tidyverse}`
[tidyverse: Easily Install and Load the 'Tidyverse'](https://cran.r-project.org/web/packages/tidyverse/index.html)
* many of the packages we will, including
- wrangling with {dplyr} & {tidyr}
- visualization with {ggplot2}
- importing with {readr} & {readxl}
- working with variables with {forcats}, {lubridate} & {stringr}
```
install.packages("tidyverse")
```
### Packages: others
Other packages that we will be using include the utilities {janitor} and {glue}. We will be adding more packages to our collection as the course progresses.
Here's the code to install {janitor} and {glue}. You can copy this line, and paste it into your console and then press <ENTER> to install the packages.
```
install.packages(c("janitor", "glue")
```
[janitor: Simple Tools for Examining and Cleaning Dirty Data](https://cran.r-project.org/web/packages/janitor/index.html)
[glue: Interpreted String Literals](https://cran.r-project.org/web/packages/glue/index.html)
### Updating existing packages
Updating packages is very easy. Simply click on the Packages pane, then Update. Anything that has a newer version will be listed. You can select all, one or some, then choose "Install Updates".
![](images\02_packages_update.png)
If the following warning pops up, choose Yes:
![](images\02_install_package_warning.png)
Alternatively, if you know which package(s) you want to update, use the `install.packages()` function.
### Loading packages
To use a package's functions, we can "call" the function by naming it:
```{r}
four <- 4
```
The above calls the {base} package's `assignOps` function "<-". This operator assigns the numeric value 4 to an object called "four". It saves that object in your environment.
![](images/02_assignOps.png)
Now, use the `here()` function from the {here} package to find the value that object (i.e., the object called `four`).
```{r, eval=FALSE}
here(four)
```
You get an error:
![](images/02_error.png)
To use a package, you must have it installed on you computer (a one-time action). However, every time you open RStudio (i.e., every session), you must load a package to use it (unless it is a base package, which are loaded automatically). We use the `library()` function to load the packages, which gives us access to the functions within that package.
```{r}
library(here)
```
Now that the {here} package has been loaded, try using the `here()` function again to see where `four` is.
```{r}
here(four)
```
## Projects in RStudio {#projects}
An R project is a special and very useful way to keep your work together. The main benefit of using a project (as opposed to a bunch of unlinked scripts/RMD files and data) is that importing data is much easier: a project pre-defines a relative working directory (the place R looks for files) in the folder containing the .Rproj file.
If you do NOT have an R project set up, you will need to set the working directory, which means (probably long) path names that may change over time and that are sensitive to the computer and it's operating system (e.g., PCs and Macs use different slashes in file paths; different computers may have different drives, etc.). This will save time for you down the road or others who may need
to reproduce your work.
Another benefit is that R projects can allow for version control and collaboration (i.e., GitHub).
### How to create a project:
1. Open RStudio.
2. Click on File and choose "New Project".
3. Choose "New Directory".
![](images\02_create_proj1.png){width=75%}
4. Choose "New Project".
![](images\02_create_proj1a.png){width=75%}
5. Name the project (i.e., directory name) and browse to where you want it. In this example, the directory name is the name of the course ("UVIC_BIDA302") and the directory is going to be in the C:/ drive.
5b. IF you want this to be a GitHub repository, check the "Create a git repo" box.
6. Check the box "Open in new session" at the bottom left.
7. Click "Create Project" button.
![](images\02_create_proj2.png){width=75%}
Your newly created project will open in a new RStudio session. It will have created an .Rproj file.
![](images\02_create_proj3.png){width=75%}
To open this project next time, navigate to where you saved it and double-click the .Rproj file.
![](images\02_create_proj4.png)
### How to open an existing project:
If you opened RStudio from the desktop icon or start menu, you will see "Project: (None)" in the corner instead.
![](images\02_no_project.png){width=75%}
Click on "Open Project" (or click on File > "Open Project..." and browse to the .RProj you want).
Once RStudio is open, click on the file you want in the Files pane (e.g., "02_projects_and_packages.Rmd").
![](images\02_open_script.png)
### How to tell if you are in a project:
If you opened this file by opening the project and then clicking on the file, you should see "UVIC_BIDA302" in the far right top corner. A down arrow beside it allows you to start a new project, close the project, or open a different project, etc.
![](images\02_create_proj3.png)
### Further reading
For more about RStudio projects:
* the chapter ["Workflow: scripts & projects" in _R For Data Science_, 2nd ed.](https://r4ds.hadley.nz/workflow-scripts.html#projects)
* ["Creating a New RStudio Project" in the _R Cookbook (2nd ed.)_](https://rc2e.com/navigatingthesoftware#recipe-CreateProject)
-30-