/
quickstart-startup.qmd
168 lines (101 loc) · 7.83 KB
/
quickstart-startup.qmd
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
# Getting started with R
This chapter helps get you acclimated to the screens of RStudio and the basic structure of R commands. You will always work in RStudio (the car), not R (the engine).
## Getting to know the RStudio screen
::: callout-dothis
Open the application called RStudio ![rstudio](assets/images/r-start-rstudio-yes.png),\
NOT R ![not r](assets/images/r-start-no-r.png).
:::
This is what your screen probably looks like:
::: column-body-outset-right
![console](assets/images/r-start-rstudio.png){style="border: none;" width="100%"}
:::
### The Console {.unnumbered}
The Console is where you can type commands and interact directly with the programming language. Think of it as a very powerful calculator at first. One reason to use it is to install packages.
If you followed the installation demo, you've already used the console to install one package using the command `install.packages("tidyverse")`. Go back and do that part now if you skipped it. It might take several minutes to finish.
### Files tab {.unnumbered}
We won't be using many of the tabs in the lower right, but the Files tab can help you if you're having trouble navigating your work. Under the More button, you can choose "Go to working directory", since that's where R thinks you've parked all of your work. This can be confusing in R, which is why we'll be working in "projects" that bundle up all of your work in one place. (I don't find the Help sections in this area very helpful. Instead, I usually rely on written manuals elsehwere, such as [tidyverse](https://www.tidyverse.org/) official site. )
### Environment
The upper right screen is the Environment, which is where your active objects live. An object, sometimes called a “variable” in R, is a named thing. It might be a word, a list of words or numbers, or a data frame (spreadsheet). It can even be a little program called a function that you write yourself.
Anything that you want to use has to be loaded or created into that environment first. That might involve importing data from Excel or a text file, or creating data yourself in a program.
### Typing into the console {.unnumbered}
When you type this: `5+5` after the \> prompt and press return, you'll see: `[1] 10`
When you type this: `"Sarah"` , you'll get this back: `[1] "Sarah"`
To create a new variable, you'll use the *assignment* operator `<-` (two characters : A less than sign and a hyphen). Here is how I would create the variable called `my_name` (lower case, no spaces). Notice how it appears in the Environment after being created. Then I can print it by typing the name of the variable instead of the letters of my name in quotes:
::: {.content-visible when-format="html"}
![](https://cronkitedata.s3.amazonaws.com/images/r-start-variable-assign.gif)
:::
::: {.content-visible when-format="pdf"}
![](assets/images/r-start-variable-assign.png)
:::
::: callout-tip
## Pro tip
Use the keyboard shortcut {{< kbd mac=Option-- win=Alt-- >}} to create the `<-` assignment operator .
:::
The console remembers your commands, but you have to type them one at a time and you can't save them for later, or start over without entering them all again. That's why we're going to work in programs called Quarto documents most of the time.[^r-start-1]
[^r-start-1]: These used to be "R Markdown". Quarto is the newer implementaiton of it, but you'll often see R Markdown referenced in other places.
## Take a tour
::: callout-dothis
Watch Andrew Heiss's tour of RStudio.
:::
::: {.mt-4 .content-visible when-format="html"}
{{< video "https://www.youtube.com/embed/cnQ-v1UUWyE" >}}
:::
::: {.content-visible when-format="pdf"}
<https://youtu.be/cnQ-v1UUWyE>
:::
We will not be using the R scripts he shows in the end of the video, but many other examples and resources do. Also, as we get into working with Quarto, the "code chunks" work the same way as a script.
## Unlocking packages and the tidyverse
The real power of R comes with packages. Packages are bundles of programs that others have found useful to extend the base R language. R is almost useless to a normal person without them. There are more than 10,000 packages available for R, each doing a special job.
If you followed along with the tutorial in the last chapter, the final step was to install a package called the `tidyverse`. Almost everything we do from now on depends on that.
The tidyverse is a system of packages, or libraries, that work together with similar grammar and syntax. It's particularly useful for the kind of work reporters do -- importing, cleaning and analyzing data that we get from others. For many reporters (myself included), R was too difficult to learn before the tidyverse came around. We'll be working almost exclusively within the tidyverse in this course.
::: callout-tip
### Searching for help
When you search for help, be sure to put "tidyverse" somewhere in your query. If you don't , you're likely to get inscrutible answers.
[perplexity.ai](https://perplexity.ai) and [ChatGPT](https://chat.openai.com) are two artificial intelligence sites that can help you with your code. These AI sites are particularly good at troubleshooting code -- describe your data, copy and paste your code, and ask it what's wrong! I tend to use the Perplexity site. Some of the methods shown in this book are newer than the ChatGPT cutoff date.^[If you have a Github account, you might be able to use Github Copilot in RStudio. I've found it to be more annoying than helpful.]
Either one is usually better than just Googling. If you do use Google, be sure to set a date cutoff -- R has changed a LOT in the last five years.
:::
### Installing packages for this course
We need to update your packages and install a few more for our work.
If this is the first time you've installed it, the tidyverse will take a while to install, and it will look like it's just stopped. Be patient. It can take 5 minutes or more.
You only have to install packages once on each machine you use. Later on, you'll learn how to invoke them one by one.
#### Option 1: Use the console
::: callout-dothis
Copy these commands into your Console. This list of packages contains all of the packages used in this book -- I think! Don't worry about what they all mean -- we'll use them later on.
* If it asks you if you want to restart, say "Yes".
* If it asks you if you want to build from source, say "No".
```{r}
#| eval: false
update.packages(ask=FALSE)
install.packages(c("tidyverse", "janitor" ,
"lubridate", "quarto", "swirl", "pacman",
"DT", "reactable", "gt",
"leaflet", "sf", "tmap",
"gapminder", "plotly",
"gtsummary", "tigris"
))
```
:::
#### Option 2: Use RStudio "Packages" tab
::: callout-dothis
Under the Packages tab in the lower right panel:
- Start by updating all of the packages you might already have installed by pressing the "Update" button.
- Under the Install tab, search for the packages listed in the `install.packages()` code above.
::: {.content-visible when-format="html"}
![packages](https://cronkitedata.s3.amazonaws.com/images/r-start-packages.gif)
:::
::: {.content-visible when-format="pdf"}
![packages](assets/images/r-start-packages.png)
:::
:::
#### Optional hands-on tutorial:
One of the packages you just installed is called `swirl`, which has some interactive tutorials. The first one might be useful.
::: callout-dothis
Make sure your cursor is in the Console and type `swirl::swirl()`.
Select the first chapter called *R Programming: The basics of programming in R*, and just the first section, called *Basic Building Blocks*.
:::
## Relax!
::: {#fig-relax}
![](assets/images/r-start-sylwia-bartyzel.jpeg)
Relax by Silwia Bartyzel via Unsplash
:::
You're all set up and we're ready to start programming. Congratulate yourself - everything is new, nothing is intuitive and the screen is intimidating. You've come a long way.