forked from psu-spatial/Geog364_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_Tut2_startup.Rmd
More file actions
136 lines (76 loc) · 6.06 KB
/
Copy pathpg_Tut2_startup.Rmd
File metadata and controls
136 lines (76 loc) · 6.06 KB
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
---
title: "Tutorial 2: Getting started"
subtitle: <h5 style="font-style:normal">GEOG-364 - Spatial Analysis</h4>
output:
html_document:
toc: true
toc_depth: 4
toc_float: true
theme: flatly
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message = FALSE)
```
## A: R-Basics and Changing settings
Everything in this tutorial is a one-off to get R and R-studio set up.
**First, in an easy to access place on your computer, make a folder called GEOG-364. This is where all your labs and projects are going to live.**
### i. Opening R-Studio
**Now everything is installed, open R-studio** (NOT R!). If you are using R-Studio cloud, you should in your workspace. Make a new R-Project and call it GEOG364_Lab1_PROJECT (or whatever lab you are working on)
```{r, tut2afig1, echo=FALSE,out.width="80%",fig.align='center'}
knitr::include_graphics('./index_images/pg_Tut2_startup_fig1.png')
```
You will be greeted by three panels:
- The interactive R console (entire left)
- Environment/History (tabbed in upper right)
- Files/Plots/Packages/Help/Viewer (tabbed in lower right)
```{r, tut2afig2, echo=FALSE}
knitr::include_graphics('./index_images/pg_Tut2_startup_fig2.png')
```
If you wish to learn more about what these windows do, have a look at this resource, from the Pirates Guide to R: https://bookdown.org/ndphillips/YaRrr/the-four-rstudio-windows.html.
<p class="comment">If you have used R before, you might see that there are variables and plots etc already loaded**. It is always good to clear these before you start a new analysis. To do this, click the little broom symbol in your environment tab</p>
### ii. Adjusting the four panes
Look at the VERY TOP OF THE SCREEN.
- In the`View` menu, there is a `/Panes/Pane Layout` menu item, where you can move the on-screen quadrants around. I tend to like the console to be top left and scripts to be top right, with the plots and environment on the bottom - but this is personal choice.
- There should also be a menu called `Help`<br>Useful for R-studio version and Markdown cheatsheets.
<br>
## Changing settings
R-studio wants to be helpful and will try to re-load exactly where you were in a project when you log back in. This can get confusing, so we are going to turn this off.
- **ON A MAC:** Click on the R-studio menu button on the top left of the screen, then click Preferences.
- **ON A PC/R-Studio Cloud:** Click on Tools-> Global Options -> Preferences
Now:
- UNCLICK "Restore most recently opened project at startup"
- Set "Save workspace to .RData on" exit to Never
- You can also click the appearances tab to change how the screen looks.
## B: Packages {#Tut2B_Packages}
### i. What are packages? {#Tut2Ba_whatarethey}
As described in Tutorial 3, we programme in R by asking a series of commands. R is open source meaning anyone can create a command. Over the last 20 years, millions of commands have been written.
These tend to be grouped together into collections called `Packages` or `Libraries` (two names for the same thing). They are similar to apps on your phone. For example, one package contains the complete works of Shakespeare; allows interactive website design; another allows advanced Bayesian statistics.
There is a package for literally everything and there are now about 20,000 packages available. You can see the full list here: https://cran.r-project.org/web/packages/available_packages_by_name.html
This is far too many to store on your computer, so most live on the internet in an online (free) "Package Store". You can download the ones you want, ready to load later.
So to access the commands in a package we need these two steps:
1. ONCE ONLY: Download the package from the internet
2. EVERY TIME: Load the packages you want
<p class="comment">**A close analogy is your phone:** There are millions of apps available from banking, to 50 different calendar apps. You don't have every app in the world installed on your phone - and you don't have every app you *do* download running at the same time. Instead you download the apps that you think you will need (occasionally downloading a new one on the fly) - and when you need to use an app, you click on it to open.</p>
### ii. How to download/install packages {#Tut2Bb_howtodownload}
Look at the Packages tab next to the plot one, you can see which packages/libraries are already pre-installed onto your computer.
If the package you want isn't listed:
- Click the INSTALL button in the Packages tab, then start typing the package name and it will show up (check the include dependencies box).
OR
- In the console, run the `install.packages()` command on the package you want to download **with quotes around the package name** e.g.
```{r,eval=FALSE}
install.packages("bardr")
```
OR
- R will sometime tell you that you are missing a package (sometimes a little yellow ribbon - say yes to install!)
*Note, if you run this command mulitple times, or the packages is already loaded, R-Studio might want to restart and sometimes gets confused. If it keeps asking, close R-studio, reopen and try again. If it really doesn't want to work, open R itself and run in the console there.*
**Try installing the `bardr` package onto your computer**
## C. Projects {#Tut2C_Project}
<p class="comment">**You need to make a new project before you start EVERY lab!**</p>
### i. What are R-projects? {#Tut2Ba_RProject}
An R-project is a special folder that will store everything to do with each lab in one place on your computer. This is incredibly useful - it means that if you switch from R-Cloud, to the lab computers, to your laptop, all you have to do is to move the folder and everything will just work. Learn more here.
[](https://www.linkedin.com/learning/learning-the-r-tidyverse/why-should-you-use-projects-in-rstudio?u=76811570 "Why use R Projects")
### ii. Creating a R-project {#Tut2Bb_CreateRproj}
This step is needed for EVERY lab.
##### **On R-Studio Cloud:**
Note, if you are running R-Studio Cloud, you simply need to click "Create Project" and name it.
<br>