In this practical you will get acquainted with R
,
RStudio
and R Markdown
.
In general for all practicals in this course: aim to make the exercises without looking at the answers and use the solutions file (“Practical A: solutions”) to evaluate your work. However, do not ‘struggle’ too long because you will run out of time.
In any case, ask for help when you feel help is needed.
R Project
R Markdown
file with the
exercises and empty code chunks where you can type your answers.RStudio
and choose File -> New Project. Choose
the option “Existing Directory”. Find the Project Working Directory, the
folder “RSummer2023” you just created. Click on “Create project”.RStudio
and
R Markdown
RStudio
RStudio is divided in 4 panes, as shown in lecture A, slide 9 (https://laurencefrank.github.io/R/Contents/Material/Part%20A%20-%20Introduction/Lecture_A.html):
R Markdown
This is an R Markdown
file. Markdown
is a
simple formatting syntax for authoring HTML, PDF, and MS Word documents.
A Markdown
file has three components:
R Markdown
To learn more about R Markdown
and to get help with the
options, see: Markdown Quick Reference in RStudio
, see menu
Help -> Markdown Quick Reference. This will open the reference
document in the output pane in the tab “Help”.
R Markdown
plain
textTake a look at the Markdown Quick Reference in R Studio
and try the following:
html
file. Click on the Knit to HTML
as the output format. Verify how the headers
and the hyperlink look like. Instead of knitting, it is also possible to
get a preview by clicking on “Visual” in the left upper corner of the
editor pane. Try both options.The benefit of using html as an output format lies in the dimensional properties of a web-page. Especially when dealing with long code-files, large output from analyses or many graphs, exporting your file as html is much more convenient. You can simply scroll down or up to see the ‘rest’, instead of having to flip through pages back and forth to compare code, graphs or output.
R Studio
menu
Help -> Cheat Sheets -> R Markdown Cheat Sheet. Read the section
about code chunks, see the section on page 1, bottom left with header
“Embed Code with knitr”.cars
data.Do you obtain the summary statistics?
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
cars
data in this new code chunk. Adapt the code chunk in
such a way that only the summary statistics (the results) will be
displayed in the knitted HTML document. Knit the document and verify the
results.Have a look at the following code chunk with R code:
a <- 100
The #
tells R
that everything that follows
in that specific line is not to be considered as code. In other words,
you can use #
to comment in your own
R
-scripts. I used #
here to elaborate that the
following line is the code from exercise 3.
The line a <- 100
assigns the value 100
to object a
. When reading that code say: “a gets value 100”
in your head. When you run your code, it will be saved.
Even if <-
is a pain to type, don’t use
=
instead, it will work, but it will cause confusion later.
Use RStudio
’s keyboard shortcut: Alt/Option + - (the minus
sign). Notice that RStudio
automatically surrounds
<-
with spaces (good code formatting practice).
Create an object a with element (value)
1
# type your code here
Verify that 1 is stored in a
# type your code here
Square a
(in a new code chunk)
# type your code here
Create b
and assign a+a
to
b
. Check if b
is indeed
a+a
.
# type your code here
ls()
The following is returned by R
ls()
## [1] "a"
A single value is the most basic object in R
. The next
step up in objects is a vector, followed by a matrix, followed by an
array. Eventually, each of these objects can be stored in a list. We
will learn about vectors, matrices and arrays later today.
Now you know how to use R
as a calculator and
R-Studio
as a typesetting device.
End of Practical 1
. Play around with R
and
R-studio
if you like.