Repository navigation
Expand file tree
/
Copy pathabout-R-and-RStudio.qmd
More file actions
249 lines (190 loc) · 11.4 KB
/
Copy pathabout-R-and-RStudio.qmd
File metadata and controls
249 lines (190 loc) · 11.4 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
# About R and RStudio {#sec-about-R-and-RStudio}
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
status("complete")
```
This chapter starts by explaining what R and RStudio are and how you can install
them on your own machine. We introduce you to working in RStudio, changing its
appearance to suit you and to the key things you need to know about R.
## What are R and Rstudio?
### What is R?
R is a programming language and environment for statistical computing and
graphics which is free and open source. It is widely used in industry and
academia. It is what is known as a "domain-specific" language meaning that it is
designed especially for doing data analysis and visualisation rather than a
"general-purpose" programming language like Python and C++. It makes doing the
sorts of things that bioscientists do a bit easier than in a general
purpose-language.
### What is RStudio?
RStudio is what is known as an "integrated development environment" (IDE) for R
made by [Posit](https://posit.co/). IDEs have features that make coding a little
easier.For example, syntax highlighting, code completion and viewers for files,
code objects, packages and plots. You don't have to use RStudio to use R but it
is very helpful.
### Why is it better to use R than a spreadsheet program?
Spreadsheet programs like Excel or googlesheets can be very useful for recording
and organising data but they are not statistical packages so although you can
carry out some analysis tasks in them they are limited, get things wrong
([Microsoft Excel’s bloopers
reel](https://www.theguardian.com/technology/2024/oct/28/microsoft-excels-bloopers-reel-40-years-of-spreadsheet-errors),
[Ten Memorable Excel
Disasters](https://sheetcast.com/articles/ten-memorable-excel-disasters)) and
[teach you bad data
habits](https://medium.com/h7w/5-common-excel-mistakes-that-break-your-data-quality-65b3b2f5a415).
Spreadsheets encourage you to do things that are [going to make analysis
difficult](https://datacarpentry.org/2015-05-03-NDIC/excel-ecology/02-common-mistakes.html).
### Why is it better to use R than a menu-driven statistics program?
- R is free and open source which it will always be available to you .
- Carrying out data analysis using coding makes everything you do reproducible
- The skills and expertise you gain through learning R are highly transferable
- much more so than those acquired using SPSS.
There are other good options such as Julia and Python and you are encouraged to
explore these. We chose R in part because of the [R community which is one of
R's greatest assets](https://ropensci.org/blog/2017/06/23/community/), being
vibrant, inclusive and supportive of users at all levels.
## Installing R and Rstudio
You will need to install both R and RStudio to use them on your own machine.
Installation is normally straightforward but you can [follow a
tutorial](https://learnr-examples.shinyapps.io/ex-setup-r/#section-welcome).
### Installing R
Go to <https://cloud.r-project.org/> and download the "Precompiled binary
distributions of the base system and contributed packages" appropriate for your
machine.
#### For Windows
Click "Download R for Windows", then "base", then "Download R 4.#.# for
Windows". This will download an `.exe` file. Once downloaded, open (double
click) that file to start the installation.
#### For Mac
Click "Download R for (Mac) OS X", then "R-4.#.#.pkg" to download the installer.
Run the installer to complete installation.
#### For Linux
Click "Download R for Linux". Instructions on installing are given for Debian,
Redhat, Suse and Ubuntu distributions. Where there is a choice, install both
`r-base` and `r-base-dev`.
### Installing R Studio
Go to <https://posit.co/download/rstudio-desktop/> and download RStudio Desktop
for your operating system.
## Packages
A R package is a collection of R functions which are related to a particular
task or set of tasks. When you install R, it comes with a set of packages known
as the **base** packages. These are **`stats`**, **`graphics`**,
**`grDevices`**, **`utils`**, **`datasets`**, **`methods`** and **`base`**. You
can a lot done with these packages! However there are many other packages that
allow you extend the functionality of R to do all manner of specialised tasks in
bioinformatics, spatial analysis, audio/image analysis and almost anything else
you can think of. These packages are written by the R community and are
available for free -- this is one of the strengths of an open source language
like R.
To use a package that is not one of the base packages you need to install it
from a package repository. The are two main repositories for R packages:
- [CRAN](https://cran.r-project.org/) (Comprehensive R Archive Network) is the
official and default repository for R packages. You can install packages from
CRAN using the `install.packages()` function in R. There is also a menu option
in RStudio to install packages from CRAN.
- [Bioconductor](https://www.bioconductor.org/) is a repository specifically for
bioinformatics packages. You can install packages from Bioconductor using the
`BiocManager::install()` function in R.
You can also install packages from other sources, such as GitHub. Github is a
popular platform for hosting code, often including the latest versions of
packages that have not yet been submitted to CRAN. Many developers release
experimental or in-progress packages on GitHub with the newest features.
In the next chapter you will install your first package, the **`tidyverse`**
[@tidyverse], which is a collection of packages that are designed to work
together to make data analysis easier and more intuitive.
## Introduction to RStudio
In this section we will introduce you to working in RStudio. We will explain the
windows that you see when you first open RStudio and how to change its
appearance to suit you.
### Changing the appearance
When you first open RStudio it will display three panes and have a white
background @fig-rstudio-first-open
::: {#fig-rstudio-first-open}

**Screenshot of RStudio the first time you open it.** When you first open
RStudio it will be white with three panes. The *Console* is on the left, with
the *Environment* and *Files* panes on the right.
:::
We will talk more about these three panes soon but first, let's get into
character - the character of a programmer! You might have noticed that people
comfortable around computers are often using dark backgrounds. A dark background
reduces eye strain and often makes "code syntax" more obvious making it faster
to learn and understand at a glance. Code syntax is the set of rules that define
what the various combinations of symbols mean. It takes time to learn these
rules and you will learn best by repeated exposure to writing, reading and
copying code. You have done this before when you learned your first spoken
language. All languages have syntax rules governing the order of words and we
rarely think about these consciously, instead relying on what sounds and looks
right. And what sounds and looks right grows out repeated exposure. For example,
\~45% of languages, including English, Chinese, Yoruba and Polish use the
Subject-Verb-Object syntax rule:
- English: Emma likes R
- Chinese: 艾玛喜欢R Àimǎ xǐhuān R
- Yoruba: Emma fẹ́ràn R
- Polish: Emma lubi R
and 40% use Subject-Object-Verb including Turkish and Korean
- Turkish: Emma R'yi seviyor
- Korean: 엠마는 R을 좋아한다 Emmaneun R-eul johahanda
You learned this rule in your language very early, long before you were
conscious of it, just by being exposed to it frequently. In this book I try to
tell you the syntax rules, but you will learn most from looking at, and copying
code. Because of this, it is well worth tinkering with the appearance of RStudio
to see what Editor theme makes code elements most obvious to you.
There is a tool bar at the top of RStudio. Choose the `Tools` option and then
`Global options`. This will open a window where many options can be changed
@fig-tools-global-options-appearance.
::: {#fig-tools-global-options-appearance}

**Screenshot of RStudio's Global Options window showing the Appearance options**
Choosing Tools \| Global Options opens a window where many things, listed on the
left, can be customised. You can change the theme and the font under
*Appearance*
:::
Go to the `Appearance` Options and choose and Editor theme you like, followed by
OK.
The default theme is called Textmate. You will notice that all the Editor themes
have syntax highlighting so that keywords, variable names, operators, etc are
coloured. Some themes have stronger contrasts than others. For beginners, I
recommend Vibrant Ink, Chaos or Merbivore rather than Dreamweaver or Gob which
have little contrast between some elements. However, individuals differ so
experiment for yourself. I tend to vary between Solarised light and dark.
You can also turn one Screen Reader Support in the Accessibility Options in
Tools \| Global Options.
Back to the Panes. You should be looking at three windows: One on the left and
two on the right[^about-r-and-rstudio-1].
[^about-r-and-rstudio-1]: If this is not a fresh install of RStudio, you might
be looking at four windows, two on the left and two on the right. That's
fine - we will all be using four shortly. For the time being, you might want
to close the "Script" window using the small cross next to "Untitled1".
The window on the left, labelled *Console*, is where R commands are executed. In
the next chapter we will start by typing commands in this window. Over on the
right hand side, at the top, are several tabs, with the *Environment* tab
showing. This is where all the objects and data that you create will be listed.
Behind the *Environment* tab is the *History* tab and later you will be able to
view this to see a history of all your commands.
On the bottom right hand side, we have a *Plots* tab, which is where your plots
will go, a *Files* tab which is a file explorer just like Windows Explorer or
Mac Finder, and a *Packages* tab where you can see all the packages that are
currently installed. The Packages tab also provides a way to install additional
packages. The *Help* tab has access to all the manual pages.
## Summary
1. **R** is a free, open-source programming language for statistical computing
and graphics. **RStudio** is an integrated development environment (IDE)
that provides a user-friendly interface for working with R, offering
features like syntax highlighting, auto-completion, and plot viewers.
2. R enables reproducible, script-based data analysis—more robust and flexible
than spreadsheets or menu-driven tools like SPSS. It is especially
well-suited for scientific research and has wide adoption in academia and
industry.
3. R is installed from [CRAN](https://cloud.r-project.org/), and RStudio is
installed from [Posit](https://posit.co/download/rstudio-desktop/). The
process varies slightly for Windows, macOS, and Linux users.
4. R’s capabilities are extended by **packages**—collections of functions for
specific tasks. Packages can be installed from **CRAN**, **Bioconductor**
(for bioinformatics), or GitHub. The `install.packages()` and
`BiocManager::install()` functions are commonly used.
5. RStudio includes panes for the **Console**, **Environment**, **Files**,
**Plots**, **Packages**, and **Help**. Users can customize the interface
(e.g., themes for better syntax visibility). Learning R syntax comes best
from repetition and experimentation.