-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path_121_rstudio_project.Rmd
214 lines (109 loc) · 7.04 KB
/
_121_rstudio_project.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
<!-- This file by Julie Hawkins
is licensed 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://github.com/EvaMaeRey/tidyverse_in_action) -->
```{r setup_121, echo = FALSE}
library(knitr)
opts_chunk$set(message = FALSE, warning = FALSE, cache = TRUE)
options(width = 100, dplyr.width = 100)
```
# 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: projects" in _R For Data Science_](https://r4ds.had.co.nz/workflow-projects.html)
* ["Creating a New RStudio Project" in the _R Cookbook (2nd ed.)_](https://rc2e.com/navigatingthesoftware#recipe-CreateProject)
## Packages
<https://www.statmethods.net/interface/packages.html>
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`, `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()
```
## `base` package
Get the documentation on the `base` package by clicking on "base" in the Packages pane.
Clicking on any of the underlined (linked) words brings up the associated help page. Scroll down and click on `assignOps`.
![](images\02_packages.png)
Help pages tell you the name of the function and which package it is in. The next line is the function's descriptive name, followed by a Description, Usage, Arguments, Details, Value, References, See Also, and Examples (if applicable). If you click on "Index" on the last line, it brings you back to the package's documentation.
In the documentation, if you click on Misc (or scroll near the bottom), you will find "<-", which will take you to the same help page as when you clicked on `assignOps`.
Or, type "assignOps" or the first few letters in the Help search box, or double-click on a function name and press F1, or, call a function's help page with `?assignOps`.
```{r, eval=FALSE}
?assignOps
```
## 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:
```{r, eval=FALSE}
install.packages("base")
```
## 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:
```{r, eval=FALSE}
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)
## Loading packages
To use a package's functions, just call it:
```{r}
four <- 4
```
The above calls the `base` package's `assignOps` function "<-" which assigns "4" to a Value called "four". It saves in your environment.
![](images/02_assignOps.png)
Now, use the `here` package to find that file (i.e., the value called `four`).
```{r, eval=FALSE}
here(four)
```
You just get an error:
![](images/02_error.png)
To use a package, you must have it installed (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).
```{r}
library(here)
```
Now, try using the `here` package again to see where `four` is.
```{r}
here(four)
```
-30-