-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_04-Tutorial2_Libraries.Rmd
160 lines (92 loc) · 6.14 KB
/
in_04-Tutorial2_Libraries.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
```{r setup, include=FALSE,message=FALSE,warning=FALSE}
# OPTIONS -----------------------------------------------
knitr::opts_chunk$set(echo = TRUE,
warning=FALSE,
message = FALSE)
# Tutorial packages
library(vembedr)
library(skimr)
library(yarrr)
library(RColorBrewer)
library(GGally)
library(tidyverse)
library(plotly)
library(readxl)
library(rvest)
library(biscale)
library(tidycensus)
library(cowplot)
library(units)
```
# Libraries/Packages {#T2_Libraries}
## What are packages? {#T2_Libraries_about}
R is open source, so over the last 20 years, *millions* of useful commands have been written that we might want to use. To make life easier, commands are grouped together into collections called `Packages` or `Libraries` (two names for the same thing). For example, one package might make pretty plots, another might focus on efficient Bayesian analysis.
<br>
------------------------------------------------------------------------
- ***A close analogy is your phone:** There are millions of apps now available from banking, to social media, to calendars and games.*
- *But! You don't have every app in the world installed on your phone - Instead you download the apps that you think you will need (occasionally downloading a new one on the fly) -*
- *You also don't have all the apps you downloaded running at the same time on your phone. When when you need to use an app, you click on it to open.*
Just like your phone, to access the commands in a package we need two steps:
1. **ONCE ONLY: Download the package from the internet**
2. **EVERY TIME: Load/Open the packages you want**
------------------------------------------------------------------------
<br>
## Seeing what packages you already have
Some packages are downloaded on your computer by default (just like the flashlight /calculator app on your phone). You can see this list in the Package tab.
```{r,im_T2_Packages, echo=FALSE, out.width="80%",fig.align='center'}
knitr::include_graphics('./index_images/im_T2_Packages.png')
```
<br>
------------------------------------------------------------------------
## The app store/getting new packages
There is a package for literally everything and there are now well over 20,000 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.<br>
<br>
To download/install a new package
### Manually click
This is like going to the app store to get a new app. Just like you only go to the app store once, this is a one-off for each package. NOTE! For R studio cloud online, you might have to do this for each project.
- Look for the quadrant with the packages tab in it.
- You will see a list of packages/apps that have already been installed.
- Click the INSTALL button in the Packages tab menu (on the left - see figure above)
- Start typing the package name and it will show up (check the include dependencies box). Install the package.
<br>
### Little yellow banner
- R will sometime tell you that you are missing a package (sometimes a little yellow ribbon), click install to install!
```{r, im_T2_yellowbanner, echo=FALSE, fig.align='center',out.width="100%"}
knitr::include_graphics('./index_images/im_T2_yellowbanner.png')
```
<br>
### Problem solving
#### Why isn't my package downloading? Its frozen
Sometimes R will ask you if you want to install binaries or other things. IT WILL ASK YOU THIS QUESTION THROUGH "SPEAKING" IN THE CONSOLE. It expects you to type yes or no, and to press enter to continue. Try yes, if it doesn't work (esp xfun), try no. <br><br>
#### R keeps asking to restart.
Sometimes R-Studio might want to restart when downloading packages and occasionally gets confused. If it keeps asking, press cancel, then go to the Session menu at the VERY top, click Restart R and Clear output, reopen and try again.
<br>
------------------------------------------------------------------------
## Using/Loading a package you 'own'
Just as going to the app store doesn't check your credit-card balance or make an Instagram post, simply *downloading* a package from the app-store doesn't make the commands immediately available. For that you need to load it (just as you click on a phone app to open it).
**I will tell you which packages you need for each lab, but if R tells you it wants a package, then install it AND load it.**
This can be done with the `library()` command.
For example, this command loads the full works of Shakespeare from the the bardr package. (<https://www.rdocumentation.org/packages/bardr/versions/0.0.9>). If you want to try this, you will need to first install bardr using the instructions above.
```{r, eval=FALSE}
library(bardr)
```
<br>
### Using a single command from a package
ADVANCED: Sometimes several packages name a command the same word and you want to specify which package you want to use.
You can do this using the :: symbol. For example, this command *forces* the computer to use the 'dplyr package' version of filter.
```{r, eval=FALSE}
dplyr::filter(mydata)
```
<br>
### Problem Solving
#### Nothing happened!
If you have managed to load a package successfully, often nothing happens - this is great! It means it loaded the package without errors.
#### There was a load of text output - an error?
Hard to tell. So I suggest running the library command TWICE! This is because many packages will print "friendly messages" or "welcome text" the first time you load them.
For example, this is what shows up when you install the tidyverse package. The welcome text is indicating the sub-packages that tidyverse downloaded and also that some commands now have a different meaning.
```{r, im_T2_friendlytext, echo=FALSE, fig.cap = "Tidyverse install messages",fig.align='center',out.width="100%"}
knitr::include_graphics('./index_images/im_T2_friendlytext.png')
```
**To find out if what you are seeing is a friendly message or an error, run the command again. If you run it a second time and and nothing happens then you're fine.**
<br>