forked from PsyTeachR/ads-v2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01-intro.qmd
More file actions
590 lines (374 loc) · 32.7 KB
/
01-intro.qmd
File metadata and controls
590 lines (374 loc) · 32.7 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Introduction {#sec-intro}
<div class="right meme"><img src="images/memes/rstudio.png" alt="A line drawing of a person looking at a computer with a magnifying glass. The text reads 'I just installed RStudio. I'm a data scientist now.'" /></div>
## Intended Learning Outcomes {#sec-ilo-intro - .ilo}
- [ ] Install R and RStudio
- [ ] Install add-on packages
- [ ] Get help for packages and functions
- [ ] Create objects by writing and running code in the console
## Functions used {#sec-functions-intro -}
```{r}
#| include: false
library(beepr)
library(devtools)
```
* built-in (you can always use these without loading any packages)
* base:: `.rs.restartR()`, `as.Date()`, `library()`, `paste()`, `sample()`, `Sys.Date()`
* utils:: `?`, `install.packages()`, `vignette()`
* other (you need to load each package to use these)
* beepr:: `beepr::beep()`
* devtools:: `devtools::install_github()`
## Setup {#sec-setup-intro -}
::: {.callout-tip .activity}
Download the [RStudio IDE Cheatsheet](https://rstudio.github.io/cheatsheets/html/rstudio-ide.html)
:::
## Installing R and RStudio {#sec-intro-installing-r}
R is a programming language that you will write code in and RStudio is a program that makes working in R easier.
::: {.callout-tip .activity}
Install R and RStudio on your computer. We have written [a guide](https://psyteachr.github.io/RSetGo/) for how to do this on Windows and Mac and also how to use RStudio on a UofG desktop computer.
Once you have installed R and RStudio, come back to this chapter. If you already had R and/or RStudio installed, we recommend updating to the latest version before you work through this course - there is also [a guide](https://psyteachr.github.io/RSetGo/updating-r.html) for how to do this.
:::
### RStudio {#sec-rstudio_ide}
When you installed R, that gave your computer the ability to process the R programming language, and also installed an app called "R". We will never use that app. Instead, we will use [RStudio](http://www.rstudio.com).
::: {.callout-warning}
## Launch R though the RStudio IDE
Launch <img src="images/intro/rstudio_icon.png" style="height: 2em; vertical-align: middle;" alt="RStudio.app" />, not <img src="images/intro/new_R_logo.png" style="height: 2em; vertical-align: middle;" alt="R.app" />.
:::
RStudio is an Integrated Development Environment (`r glossary("IDE")`). Think of it as using a plain text editor like NotePad to write a book versus using a word processor like Microsoft Word. You could do it, but it would be much harder without things like spell-checking and formatting and you wouldn't be able to use some of the advanced features that Word has developed. In a similar way, you can use R without R Studio but we wouldn't recommend it. (You can use your favourite IDE, like VSCode, but the examples in this book will all be shown in RStudio.) RStudio serves as a text editor, file manager, spreadsheet viewer, and more. The key thing to remember is that although you will do all of your work using RStudio for this course, you are actually using two pieces of software, which means that from time to time, both of them may have separate updates.
RStudio is arranged with four window `r glossary("panes")`.
{#fig-rstudio fig-alt="An interface with 4 panes: code in the upper left and lower right, object info in the upper right, and a plot in the lower right"}
By default, the upper left pane is the **source pane**, where you view, write, and edit code from files and view data tables in a spreadsheet format. When you first open RStudio, this pane won't display until we open a document or load in some data -- don't worry, we'll get to that soon.
The lower left pane is the **console pane**, where you can type in commands and view output messages. You can write code in the console to test it out. The code will run and can create objects in the environment, but the code itself won't be saved. You need to write your code into a script in the source pane to save it, which we'll cover in @sec-reports.
The right panes have several different tabs that show you information about your code. The most used tab in the upper right pane is the **Environment** tab, which lists some information about the `r glossary("object", "objects")` that you have defined in your code.
In the lower right pane, the most used tabs are the **Files** tab for directory structure, the **Plots** tab for plots made in a script, the **Packages** tab for managing add-on packages (see @sec-packages), the **Viewer** tab to display reports created by your scripts, and the **Help** tab to see function help. We'll learn more about the Help tab in @sec-function-help.
You can change the location of panes and what tabs are shown in each pane under <if>Tools > Global Options... > Pane Layout</if>.
### Reproducibility {#sec-intro-reproducibility}
In this class, you will be learning how to make reproducible reports. This involves writing scripts that transform data, create summaries and visualisations, and embed them in a report in a way that always gives you the same results.
When you do things reproducibly, others (and future you) can understand and check your work. You can also reuse your work more easily. For example, if you need to create a report every month with the social media analytics for your company, a reproducible report allows you to download a new month's data and create the report within seconds. It might take a little longer to set up the report in the first instance with reproducible methods, but the time it saves you in the long run is invaluable.
## RStudio Settings {#sec-rstudio-settings}
There are a few settings you should fix immediately after updating RStudio. While the default settings restore the objects from your last session every time you reopen RStudio, if you keep objects around in your workspace, things will get messy, and unexpected issues will occur. You should always start with a clear workspace. This also means that you never want to save your workspace when you exit; the only thing you want to save are your scripts.
::: {.callout-tip .activity}
Go to <if>Tools > Global Options...</if> (<mac>Cmd-,</mac>):
* In the General tab, uncheck the box that says Restore .RData into workspace at start-up.
* Change "Save workspace to .RData on exit" to "Never".
You will need to change these settings each time you update RStudio.
:::
### Themes and accessiblilty
You can customise how RStudio looks to make it work for you. You can change the default font, font size, and general appearance of RStudio, including using dark mode.
::: {.callout-tip .activity}
Click <if>Tools > Global Options > Appearance</if>. Play around with the settings and see what you prefer -- you're going to spend a lot of time with R, it might as well look nice!
:::
## Sessions {#sec-intro-sessions}
If you have the above settings configured correctly, when you open up RStudio and start writing code, loading packages, and creating objects, you will be doing so in a new session and your Environment tab should be completely empty. If you find that your code isn't working and you can't figure out why, it might be worth restarting your R session. This will clear the environment and detach all loaded packages - think of it like restarting your phone. There are several ways that you can restart R:
* Menu: <if>Session > Restart R</if>
* <mac>Cmd-Shift-F10</mac> or <pc>Ctl-Shift-F10</pc>
* type `.rs.restartR()` in the console
::: {.callout-tip .activity}
Try each method of restarting R. Additionally, now would be a good time to start your reflective log where you can keep a record of useful hints and tips and things to try when your code isn't working. Add "restart R session" to this notebook as your first item.
:::
## Packages and functions {#sec-packages}
When you install R you will have access to a range of `r glossary("function", "functions")` including options for `r glossary("data wrangling")` and statistical analysis. The functions that are included in the default installation are typically referred to as `r glossary("base R")` and you can think of them like the default apps that come pre-loaded on your phone.
One of the great things about R, however, is that it is **user extensible**: anyone can create a new add-on that extends its functionality. There are currently thousands of `r glossary("package", "packages")` that R users have created to solve many different kinds of problems, or just simply to have fun. For example, there are packages for data visualisation, machine learning, interactive dashboards, web scraping, and playing games such as Sudoku.
Add-on packages are not distributed with base R, but have to be downloaded and installed from an archive, in the same way that you would, for instance, download and install PokemonGo on your smartphone. The main repository where packages reside is called `r glossary("CRAN")`, the Comprehensive R Archive Network.
There is an important distinction between **installing** a package and **loading** a package.
### Installing a package {#sec-install-package}
::: {.meme .right}
{fig-alt="Pikachu and Eevee from Pokemon waving and high-five-ing"}
:::
This is done using `install.packages()`. This is like installing an app on your phone: you only have to do it once and the app will remain installed until you remove it. For instance, if you want to use PokemonGo on your phone, you install it once from the App Store or Play Store; you don't have to re-install it each time you want to use it. Once you launch the app, it will run in the background until you close it or restart your phone. Likewise, when you install a package, the package will be available (but not *loaded*) every time you open up R.
::: {.callout-tip .activity}
Install the <pkg>tidyverse</pkg> package on your system. This is the main package we will use throughout this book for data wrangling, summaries, and visualisation. It is actually a bundle of packages, which we'll explain further in @sec-tidyverse.
```{r}
#| label: install-pckg
#| eval: false
#| code-summary: "Run in the console"
install.packages("tidyverse")
```
:::
If you get a message that says something like `package ‘tidyverse’ successfully unpacked and MD5 sums checked`, the installation was successful. If you get an error and the package wasn't installed, Google the error message or use AI to help identify the problem.
::: {.callout-caution}
## Install packages from the console only
Never install a package from inside a script. Only do this from the console pane or the packages tab of the lower right pane. This is because if you put the install code in a script, you might run it accidentally and re-install the package, which will update it. Sometimes you want to do this, and sometimes a package update might cause your code to stop working.
:::
::: {.callout-tip .activity}
Here are some other packages you'll want to install for the first two chapters.
```{r}
#| label: install-pckg-2
#| eval: false
#| code-summary: "Run in the console"
install.packages("beepr") # for beeps
install.packages("devtools") # for installing packages from github
```
Once you've installed the <pkg>devtools</pkg> package, you can also install packages from repositories other than CRAN, such as github. The following code installs the development version of a package for making waffle plots, which we will use later in this book.
```{r}
#| label: install-waffle
#| eval: false
#| code-summary: "Run in the console"
# install waffle package
devtools::install_github("hrbrmstr/waffle")
```
:::
### Loading a package
This is done using the `library()` function. This is like **launching** an app on your phone: the functionality is only there where the app is launched and remains there until you close the app or restart. For example, when you run `library(devtools)` within a session, the functions in the package referred to by `devtools` will be made available for your R session. The next time you start R, you will need to run `library(devtools)` again if you want to access that package.
::: {.callout-tip .activity}
After installing the <pkg>beepr</pkg> package, you can load it for your current R session as follows:
```{r}
#| label: library-pckg
#| code-summary: "Run in the console"
library(beepr)
```
:::
You might get some red text when you load a package, this is normal. It is usually warning you that this package has functions that have the same name as other packages you've already loaded.
::: {.callout-note}
You can use the convention `package::function()` to indicate in which add-on package a function resides. For instance, if you see `readr::read_csv()`, that refers to the function `read_csv()` in the <pkg>readr</pkg> add-on package. If the package is loaded using `library()`, you don't have to specify the package name before a function unless there is a `r glossary("conflict")` (e.g., you have two packages loaded that have a function with the same name).
:::
### Using a function
::: {.callout-tip .activity}
Now you can run the function `beep()` (make sure you have the sound on or your headphones plugged in).
```{r}
#| label: beepr-demo
#| eval: false
#| code-summary: "Run in the console"
beep()
```
:::
A `r glossary("function")` is a name that refers to some code you can reuse. We'll start by using functions that are provided for you in packages, but you can also write your own functions. After the function name, there is a pair of parentheses, which contain zero or more `r glossary("argument", "arguments")`. These are options that you can set. In the example above, the `sound` argument has a `r glossary("default value")` of `1`, which makes a "ping" sound.
::: {.callout-tip .activity}
Try changing the argument to an integer between 1 and 11.
```{r}
#| label: beepr-demo-2
#| eval: false
#| code-summary: "Run in the console"
beep(sound = 8)
```
:::
If you type a function into the console pane, it will run as soon as you hit enter. If you put the function in a `r glossary("script")` or `r glossary("Quarto")` document in the `r glossary("panes", "source pane")`, it won't run until you run the script, `r glossary("knit")` the Quarto file, or run a code `r glossary("chunk")`. You'll learn more about this in @sec-reports.
### Tidyverse {#sec-tidyverse}
<pkg>tidyverse</pkg> is a meta-package that loads several packages we'll be using in almost every chapter in this book:
- <pkg>ggplot2</pkg>, for data visualisation (@sec-viz)
- <pkg>readr</pkg>, for data import (@sec-import)
- <pkg>tibble</pkg>, for tables (@sec-tables)
- <pkg>tidyr</pkg>, for data tidying (@sec-tidy)
- <pkg>dplyr</pkg>, for data manipulation (@sec-wrangle)
- <pkg>stringr</pkg>, for `r glossary("string", "strings")` (@sec-data-types)
- <pkg>forcats</pkg>, for `r glossary("factor", "factors")` (@sec-factors)
- <pkg>lubridate</pkg>, for working with dates (@sec-dates-times)
- <pkg>purrr</pkg>, for repeating things
When you install <pkg>tidyverse</pkg>, it also installs some other useful packages that you can load individually. You can get the full list using `tidyverse_packages()`, but the packages we'll be using in this book are:
- <pkg>googlesheets4</pkg>, for working with Google spreadsheets
- <pkg>readxl</pkg>, for Excel files
- <pkg>hms</pkg>, for working with times
- <pkg>rvest</pkg>, for web scraping
### Function Help {#sec-function-help}
When you load the <pkg>tidyverse</pkg> it automatically loads all of the above packages, however, it can be helpful to know which package a function comes from if you need to Google it. If a `r glossary("function")` is in `r glossary("base R")` or a loaded package, you can type `?function_name` in the console to access the help file. At the top of the help it will give you the function and package name.
If the package isn't loaded, use `?package_name::function_name` or specify the package in the `help()` function. When you aren't sure what package the function is in, use the shortcut `??function_name`.
::: {.callout-tip .activity}
Use the methods above to get help for the `beepr::beep()` function.
:::
```{r}
#| label: help
#| eval: false
#| code-summary: "Run in the console"
#| webex.hide: true
# if the package is loaded
?beepr
help("beepr")
# works whether or not the package is loaded
?beepr::beep
help("beep", package="beepr")
# shows a list of potentially matching functions
??beep
```
Function help is always organised in the same way. For example, look at the help for `?beepr::beep`. At the top, it tells you the name of the function and its package in curly brackets, then a short description of the function, followed by a longer description. The **Usage** section shows the function with all of its `r glossary("argument", "arguments")`. If any of those arguments have default values, they will be shown like `function(arg = default)`. The **Arguments** section lists each argument with an explanation. There may be a **Details** section after this with even more detail about the functions. The **Examples** section is last, and shows examples that you can run in your console window to see how the function works.
::: {.callout-tip .activity}
Use function help to answer the following questions.
* What is the first argument to the `mean` function? `r mcq(c("trim", "na.rm", "mean", answer="x"))`
* What package is `read_excel` in? `r mcq(c("readr", answer="readxl", "base", "stats"))`
:::
## Arguments {#sec-arguments}
You can look up the arguments/options that a function has by using the help documentation. Some arguments are required, and some are optional. Optional arguments will often use a default (normally specified in the help documentation) if you do not enter any value.
::: {.callout-tip .activity}
As an example, look at the help documentation for the function `sample()` which randomly samples items from a list.
```{r}
#| label: help-doc
#| eval: false
#| code-summary: "Run in the console"
?sample
```
:::
The help documentation for `sample()` should appear in the bottom right help panel. In the usage section, we see that `sample()` takes the following form:
```{r}
#| label: arguments
#| eval: false
sample(x, size, replace = FALSE, prob = NULL)
```
In the arguments section, there are explanations for each of the arguments:
* `x` is the list of items we want to choose from,
* `size` is the number of items we want to choose,
* `replace` is whether or not each item may be selected more than once,
* `prob` gives the probability that each item is chosen.
In the details section it notes that if no values are entered for `replace` or `prob` it will use defaults of `FALSE` (each item can only be chosen once) and `NULL` (all items will have equal probability of being chosen). Because there is no default value for `x` or `size`, they must be specified otherwise the code won't run.
::: {.callout-tip .activity}
Let's try an example and just change the required arguments `x` and `size` to ask R to choose from the set of `letters` (a built-in `r glossary("vector")` of the 26 lower-case Latin letters), 5 random values.
```{r}
#| echo: false
# make sure values are the same
set.seed(8675309)
```
```{r}
sample(x = letters, size = 5)
```
:::
:::{.callout-note collapse="true"}
## Why are my letters different to your letters?
`sample()` generates a random sample. Each time you run the code, you'll generate a different set of random letters (try it). The function `set.seed()` controls the random number generator - if you're using any functions that use randomness (such as `sample()`), running `set.seed()` will ensure that you get the same result (in many cases this may not be what you want to do). To get the same numbers we do, run `set.seed(8675309)` in the console, and then run `sample(x = letters, size = 5)` again.
:::
::: {.callout-tip .activity}
Now we can change the default value for the `replace` argument to produce a set of letters that is allowed to have duplicates.
```{r}
set.seed(8675309)
sample(x = letters, size = 5, replace = TRUE)
```
:::
This time R has still produced 5 random letters, but now this set of letters has two instances of "k". Always remember to use the help documentation to help you understand what arguments a function requires.
### Argument names
In the above examples, we have written out the argument names in our code (i.e., `x`, `size`, `replace`), however, this is not strictly necessary. The following two lines of code would both produce the same result (although each time you run `sample()` it will produce a slightly different result, because it's random, but they would still work the same):
```{r}
#| label: argument-names
#| eval: false
sample(x = letters, size = 5, replace = TRUE)
sample(letters, 5, TRUE)
```
Importantly, if you do not write out the argument names, R will use the default order of arguments. That is, for `sample` it will assume that the first value you enter is `x`, the second value is `size` and the third value is `replace`.
If you write out the argument names, then you can write the arguments in whatever order you like:
```{r}
#| label: argument-order
#| eval: false
sample(size = 5, replace = TRUE, x = letters)
```
When you are first learning R, you may find it useful to write out the argument names as it can help you remember and understand what each part of the function is doing. However, as your skills progress you may find it quicker to omit the argument names and you will also see code examples online that do not use argument names, so it is important to be able to understand which argument each bit of code is referring to (or look up the help documentation to check).
In this course, we will always write out the argument names the first time we use each function. However, in subsequent uses they may be omitted.
### Tab auto-complete {#sec-tab-autocomplete}
One very useful feature of R Studio is tab auto-complete for functions. If you write the name of the function and then press the tab key, RStudio will show you the arguments that function takes along with a brief description. If you press enter on the argument name it will fill in the name for you, just like auto-complete on your phone. This is incredibly useful when you are first learning R and you should remember to use this feature frequently.
{#fig-autocomplete fig-alt="The code reads `sample()` and there is a drop-down menu with the options `x =`, `size =`, replace =` and prob =`. The third option is highlighted blue and there is a yellow text box reading 'replace: should sampling be with replacements? Press F1 for additional help'"}
::: {.callout-tip .activity}
Use tab autocomplete to figure out the arguments to `rnorm()`. Create a vector of 20 numbers from a normal distribution with a mean of 100 and a standard deviation of 10.
:::
```{r}
#| webex.hide: true
rnorm(n = 20, mean = 100, sd = 10)
```
## Scripts {#sec-scripts}
The main purpose of this course is to teach you how to make fully reproducible reports where your text and code are all in one document. We will start using a document type called Quarto for this purpose next week but for now, we'll just use a simple script which allows us to save our code.
::: {.callout-tip .activity}
There are several ways to create a new script:
- Click File - New File - R Script
- Click the New File icon, then click R Script
- Use the shortcut <mac>Cmd-Shift-N</mac> or <pc>Ctl-Shift-N</pc>
Once you've created your new script, hit <mac>Cmd-S</mac> or <pc>Ctl-S</pc> to save it. Name it `ads-week1-1.R` and save it somewhere sensible (on a cloud drive)
:::
Scripts are useful for when you need to save the code you're writing but you're not bothered about making a full report. By default, everything you write in a script will be treat as code If you want to add any text as notes, you must use a `r glossary("comment", "comments")`. You can add comments inside scripts with the hash symbol (`#`). R will ignore characters from the hash to the end of the line.
::: {.callout-tip .activity}
In your script, copy and paste the following code and comments:
```{r}
#| message: false
#| warning: false
# this is a comment
# this code is loading the tidyverse
library(tidyverse)
# this code will randomly generate 5 letters with replacement
sample(x = letters, size = 5, replace = TRUE)
```
To run the code in your script, use the keyboard shortcut <mac>Cmd-Enter</mac> or <pc>Ctl-Enter</pc> on the line you want to run.
:::
## Objects {#sec-objects}
A large part of your coding will involve creating and manipulating objects. Objects contain stuff. That stuff can be numbers, words, or the result of operations and analyses. You assign content to an object using `<-` or `=` (we will use `<-` in this book).
::: {.callout-tip .activity}
Add the following code to your script then run it, but change the values of `name` and `age` to your own details and change `halloween` to a holiday or date you care about.
```{r}
#| label: objects
#| code-summary: "Run in the console"
name <- "Emily"
age <- 40
today <- Sys.Date()
halloween <- as.Date("2026-10-31")
```
:::
You'll see that four objects now appear in the environment pane:
* `name` is `r glossary("character")` (text) data. In order for R to recognise it as text, it **must** be enclosed in double quotation marks `" "`.
* `age` is `r glossary("numeric")` data. In order for R to recognise this as a number, it **must not** be enclosed in quotation marks.
* `today` stores the result of the function `Sys.Date()`. This function returns your computer system's date. Unlike `name` and `age`, which are hard-coded (i.e., they will always return the values you enter), the contents of the object `today` will change dynamically with the date. That is, if you run that function tomorrow, it will update the date to tomorrow's date.
* `halloween` is also a date but it's hard-coded as a specific date. It's wrapped within the `as.Date()` function that tells R to interpret the character string you provide as a date rather than text.
::: {.callout-tip .activity}
To print the contents of an object, type the object's name in the console and press enter. Try printing all four objects now.
:::
Finally, a key concept to understand is that objects can interact and you can save the results of those interactions in new object.
::: {.callout-tip .activity}
Edit and run the following code to create these new objects, and then print the contents of each new object.
Add comments to the code to help explain what each line of code is doing.
```{r}
#| label: intro-objects
#| code-summary: "Run in the console"
decade <- age + 10
full_name <- paste(name, "Nordmann")
how_long <- halloween - today
```
:::
## Getting help {#sec-help}
You will feel like you need a *lot* of help when you're starting to learn. This won't really go away; it's impossible to memorise everything. The goal is to learn enough about the structure of R that you can look things up quickly. This is why we'll introduce specialised jargon in the glossary for each chapter; it's easier to google "convert `r glossary("character")` to `r glossary("numeric")` in R" than "make numbers in quotes be actual numbers not words". In addition to the function help described above, here's some additional resources you should use often.
### Package reference manuals
Start up help in a browser by entering `help.start()` in the console. Click on <if>Packages</if> under <if>Reference</if> to see a list of packages. Scroll down to the <pkg>readxl</pkg> package and click on it to see a list of the functions that are available in that package.
### Googling
If the function help doesn't help, or you're not even sure what function you need, try Googling your question. It will take some practice to be able to use the right jargon in your search terms to get what you want. It helps to put "R" or "tidyverse" in the search text, or the name of the relevant package, like "ggplot2".
### AI
Generative AI platforms have exploded in popularity, particularly when it comes to coding. Because of this, we have created a companion book [AITutoR](https://psyteachr.github.io/AITutoR/) to show you how to use AI responsibly to support your coding journey.
::: {.meme .right}
{fig-alt="A sign with English and Chinese. The top line of text reads 'Translate Server Error', the bottom line reads '急救英文'"}
:::
While generative AI can be a very helpful tool to support coding, we ask you not to rely on it too heavily while you are building the essential skills for coding. The exercises are meant to build your mental models for understanding data. If you don't have a good understanding of how data is structured, you cannot evaluate if AI suggestions are correct, contain subtle errors, or are wildly off base.
Coming back to the exercise analogy, while a forklift can lift heavy weights easier and faster than you can, it doesn't help your sporting performance if you get a forklift to lift weights at the gym for you. The same is true for AI and coding. The exercises we ask you to do are relatively simple for AI to solve, but it's important for you to be able to fluently solve them so that you have the necessary "fitness" to do more complex tasks.
### Vignettes
Many packages, especially [tidyverse](https://www.tidyverse.org/packages/) ones, have helpful websites with vignettes explaining how to use their functions. Some of the vignettes are also available inside R. You can access them from a package's help page or with the `vignette()` function.
```{r}
#| label: vignettes
#| eval: false
#| code-summary: "Run in the console"
# opens a list of available vignettes
vignette(package = "ggplot2")
# opens a specific vignette in the Help pane
vignette("ggplot2-specs", package = "ggplot2")
```
## Glossary {#sec-glossary-intro}
The glossary at the end of each chapter defines common jargon you might encounter while learning R. This specialised vocabulary can help you to communicate more efficiently and to search for solutions to problems. The terms below link to our [PsyTeachR glossary](https://psyteachr.github.io/glossary/), which contains further information and examples.
```{r}
#| echo: false
glossary_table()
```
## Peer Coding Exercises {#sec-exercises-intro}
If you're enrolled on PSYCH1012 Applied Data Skills, do not do these exercises until the Tuesday workshop.
For each exercise, you'll work in a pair programming team. Pair programming is when two people work on the same code with distinct, rotating roles:
- The Driver operates the keyboard, implements the next small step, narrates what they are doing.
- The Navigator reviews in real time, thinks ahead, checks naming, design, and help pages, and catches mistakes and typos.
You should work in short cycles (5-10 minutes or two or three tasks), then switch roles. You will likely feel a bit awkward doing this to begin with but you will quickly get used to it (we're going to to it every week).
There are no solutions provided for the pair programming activities. Everything you need is in this book (or very easily Google-able) so use the search function and work together to solve any problems.
::: {.callout-tip .activity}
1. Decide who is going to start as the Driver and Navigator. Remember to switch every so often! The Navigator will find it helpful to have a copy of these instructions open and read the next step to the Driver.
1. Open RStudio, type `a <- 1` in the console pane, check that it is in the Environment pane, restart R, and check that `a` has disappeared from the Environment pane. If not, figure out what setting to change so that the Environment is cleared when you restart R.
1. Increase the editor font size to 14.
1. Open a new script and save it with the name `ads-week1-2.R`
1. Which version of R do you have installed? This should be a number like x.x.x `r fitb("4.5.2")`. If the box turns green, you have the latest version.
1. Which version of RStudio do you have installed? This should be a number like xxxx.xx.x`r fitb("2026.01.0")`. If the box turns green, you have the latest version.
1. Create a character object called `name` with the current Driver's name, an object called `today` that pulls the date from the computer system, an object called `ads_report` that contains the deadline of the final report for Applied Data Skills.
1. Subtract today’s date from your target date and store it as a new object called `days_left.`
1. Use `paste()` to combine your name and the number of days into a single sentence - add this to your script.
```{r}
#| eval: false
message <- paste("Hi", name,
"— there are",
days_left,
"days until your report is due")
```
10. Print the message. How many days are there until the final report is due? `r fitb(83)` (this answer will only be correct if you complete this exercise on the 13th January).
11. Add in comments with any problems you encountered along the way, how you solved them (e.g., did you have to search for information), or anything you learned.
12. Use `beep()` to play the Mario sound.
13. Save the file and make sure you both have a copy (send it to the other person via Teams or OneDrive)
:::