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.

Create an R Project

  1. Go to: https://laurencefrank.github.io/R/ and select the tab “Monday”. There you will find the following files:
    • Two versions of the lecture: the slides and the handout version
    • “Practical A: template” is an R Markdown file with questions and empty code chunks where you can type your answers.
    • “Practical A: solutions” (a HTML file) provides the answers.
  2. Download the “Practical A: template file” and save it in a new folder with name “RSummer2023”, for example.
  3. Open 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”. If you want to learn more about R Projects, see lesson 1 of the this R Markdown crash course: https://zsmith27.github.io/rmarkdown_crash-course/lesson-1-r-project-development.html
  4. Open the file “Practical_A_template.Rmd” with File -> Open File. Follow the instructions and complete the exercises.

Introduction to RStudio and R Markdown

A closer look at 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):

  1. The editor pane, where we create R code and text. This is the pane where the current file, Practical_A_template.Rmd is located.
  2. The console pane where code is processed.
  3. The environment/history pane.
  4. The output pane where we can access our files, plots, the help files, make packages and view our data objects.

The editor pane and 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:

  1. On top of the document, a YAML part, which defines the markup of the document. YAML stands for Yet Another Markup Language/ Ain’t Markup Language.
  2. Text part, where you can create plain text.
  3. The code chunks, where you type R code (but you can also choose another programming language such as Python).

Getting help on 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”.

Exercise 1: try a few options in R Markdown plain text

Take a look at the Markdown Quick Reference and try the following:

  1. Add a Level 1 Header called “This is a Header 1” to this document.
  2. Add a Level 2 sub-header with name “Sub-header 1”.
  3. Create some plain text and mark some part of the text bold or italic.
  4. Include the hyperlink to R Markdown: The Definitive Guide (https://bookdown.org/yihui/rmarkdown/)
  5. Compile this document as a html file. Click on the alt text icon and select 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.

Solution Exercise 1:

Header 1 is created with the following Markdown code: # Header 1

Header 1

Sub-header 1 is created with the Markdown code: ## Sub-header 1

Sub-header 1

This is some plain text where part of the text is in bold font and part of the text is in italic font. A comprehensive guide on Markdown is R Markdown: The Definitive Guide.

Add a hyperlink with the following Markdown code: [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/)

Exercise 2: Code chunks in Markdown

  1. Download the R Markdown cheat sheet, see 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”.
  2. Consider the code chunk below. It contains R code that asks for the summary statistics of the cars data.
  3. Run the code by clicking on the green “play” button in the top right corner of the code chunk. 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
  1. Add a new code chunk (use the code chunk icon on top of this editor pane) to this document. Copy the R code that asks for a summary of the 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 (choose Knit -> Knit to HTML) and verify the results.
##      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

Exercise 3: Learning some R code.

Have a look at the following code chunk with R code:

# Exercise 3
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).

Insert a new code chunk and create an object a with element (value) 1

a <- 1

We can see that value 1 is assigned to a, but we cannot verify that it is indeed stored.

Verify that 1 is stored in a (in a new code chunk)

a
## [1] 1

Luckily there is nothing to worry about. The value 1 is indeed stored in object a.

Square a (in a new code chunk)

a^2
## [1] 1

Still 1, that’s nice!

Create b and assign a+a. Check if b is indeed a+a.

b <- a + a
b == a + a
## [1] TRUE

True! R is telling us that the equality we tested is indeed true. In other words, everything before the == is equal to that which is posed after the ==. Now we are talking.

Exercise 3: Inspect the contents of the global environment by typing and running ls()

The following is returned by R

ls()
## [1] "a" "b"

There is are 4 objects in the environment: a and b. If you look at the environment pane, you can quickly see the same information (i.e. there is no need to type ls()) and more, because you also see that these objects contain values.

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.