-
Notifications
You must be signed in to change notification settings - Fork 32
/
01-rstudio.Rmd
208 lines (143 loc) · 7.8 KB
/
01-rstudio.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
# Getting to know RStudio
RStudio is a company that develops and maintains several products. Their best-known product is
an IDE (Integrated development environment) for the R programming language, also called RStudio.
You can install RStudio by visiting this [link](https://www.rstudio.com/products/rstudio/download/).
There is also a server version that can be used to have a centralized version of R within, say, a
company. RStudio, the company, also develops [Shiny](https://shiny.rstudio.com/), a package to
create full-fledged web-apps. I am not going to cover Shiny in this book, since there's already
[a lot](http://shiny.rstudio.com/tutorial/) of material that you can learn from.
Once you have installed RStudio, launch it and let's go through the interface together.
## Panes
RStudio is divided into different panes. Each pane has a specific function. The gif below shows
some of these panes:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_panes.gif")
```
Take some time to look around what each pane shows you. Some panes are empty; for example the *Plots*
pane or the *Viewer* pane. *Plots* shows you the plots you make. You can browse the plots and save
them. We will see this in more detail in a later chapter. *Viewer* shows you previews of documents
that you generate with R. More on this later.
## Console
The *Console* pane is where you can execute R code. Write the following in the console:
```{r, eval=FALSE}
2 + 3
```
and you'll get the answer, `5`. However, do not write a lot of lines in the console. It is better
write your code inside a script. Output is also shown inside the console.
## Scripts
Look at the gif below:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_new_script.gif")
```
In this gif, we see the user creating a new R script. R scripts are simple text files that hold R
code. Think of `.do` files in STATA or `.c` files for C. R scripts have the extension `.r` or `.R`.
It is possible to create a lot of other files. We'll take a look at `R Markdown` files in Chapter 11.
### The help pane
The *Help* pane allows you to consult documentation for functions or packages. The gif below shows
how it works:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_help.gif")
```
you can also access help using the following syntax: `?lm`. This will bring up the documentation for
the function `lm()`. You can also type `??lm` which will look for the string `lm` in every package.
### The Environment pane
The *Environment* pane shows every object created in the current section. It is especially useful
if you have defined lists or have loaded data into R as it makes it easy to explore these more
complex objects.
## Options
It is also possible to customize RStudio's look and feel:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_options.gif")
```
Take some time to go through the options.
## Keyboard shortcuts
It is a good idea to familiarize yourself with at least some keyboard shortcuts. This is more
convenient than having to move the mouse around:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_shortcuts.gif")
```
If there is only one keyboard shortcut you need to know, it's `Ctrl-Enter` that executes a line of code
from your script. However, these other shortcuts are also worth knowing:
* `CTRL-ALT-R`: run entire script
* `CTRL-ALT-UP or DOWN`: make cursor taller or shorter, allowing you to edit multiple lines at the same time
* `CTRL-F`: Search and replace
* `ALT-UP or DOWN`: Move line up or down
* `CTRL-SHIFT-C`: Comment/uncomment line
* `ALT-SHIFT-K`: Bring up the list of keyboard shortcuts
* `CTRL-SHIFT-M`: Insert the pipe operator (`%>%`, more on this later)
* `CTRL-S`: Save script
This is just a few keyboard shortcuts that I personally find useful. However, I strongly advise you
to learn and use whatever shortcuts are useful and feel natural to you!
## Projects
One of the best features of RStudio are projects. Creating a project is simple; the gif below
shows how you can create a project and how you can switch between projects.
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_projects.gif")
```
Projects make a lot of things easier, such as managing paths. More on this in the chapter about
reading data. Another useful feature of projects is that the scripts you open in project A will
stay open even if you switch to another project B, and then switch back to the project A again.
You can also use version control (with git) inside a project. Version control is very useful, but
I won't discuss it here. You can find a lot of resources online to get you started with git.
## History
The *History* pane saves all the previous lines you executed. You can then select these lines and
send them back to the console or the script.
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_history.gif")
```
## Plots
All the plots you make during a session are visible in the *Plots* pane. From there, you can
export them in different formats.
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_plots.gif")
```
The plots shown in the gif are made using basic R functions. Later, we will learn how to make nicer
looking plots using the package `ggplot2`.
## Addins
Some packages install addins, which are accessible through the addins button:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_addins.png")
```
These addins make it easier to use some functions and you can read more about them [here](https://rstudio.github.io/rstudioaddins/#overview).
My favorite addins are the ones you get when installing the `{datapasta}` package. Read more about
it [here](https://github.com/MilesMcBain/datapasta).
There are other panes that I will not discuss here, but you will naturally discover their use as you
go. For example, we will discuss the *Build* pane in Chapter 11.
## Packages
You can think of packages as addons that extend R's core functionality. You can browse all available
packages on [CRAN](https://cloud.r-project.org/). To make it easier to find what you might be
interested in, you can also browse the [CRAN Task Views](https://cloud.r-project.org/web/views/).
Each package has a landing page that summarises its dependencies, version number etc. For example,
for the `dplyr` package: [https://cran.r-project.org/web/packages/dplyr/index.html](https://cran.r-project.org/web/packages/dplyr/index.html).
Take a look at the *Downloads* section, and especially at the Reference Manual and Vignettes:
```{r, echo=FALSE}
knitr::include_graphics("pics/packages_vignette.png")
```
Vignettes are valuable documents; inside vignettes, the purpose of the package is explained in
plain English, usually with accompanying examples. The reference manuals list the available functions
inside the packages. You can also find vignettes from within Rstudio:
```{r, echo=FALSE}
knitr::include_graphics("pics/rstudio_vignette.gif")
```
Go to the *Packages* pane and click on the package you're interested in. Then you can consult the
help for the functions that come with the package as well as the package's vignettes.
Once you installed a package, you have to load it before you can use it. To load packages you use the
`library()` function:
```{r, eval=FALSE}
library(dplyr)
library(janitor)
# and so on...
```
If you only need to use one single function once, you don't need to load an entire package. You can
write the following:
```{r, eval=FALSE}
dplyr::full_join(A, B)
```
using the `::` operator, you can access functions from packages without having to load the whole
package beforehand.
It is possible and easy to create your own packages. This is useful if you have to write a lot of
functions that you use daily. We will lean about that, in Chapter 10.
## Exercises
### Exercise 1 {-}
Change the look and feel of RStudio to suit your tastes! I personally like to move the console
to the right and use a dark theme. Take some 5 minutes to customize it and browse through all the options.