Practical A

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: Exercises” provides the exercises of the practical.
    • “Practical A: template” is an R Markdown file with the exercises 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”.
  4. Open the “Practical_A_template.Rmd” with File -> Open File.
  5. Go to the course website, select the tab “Monday” and click on “Practical A: Exercises” to get the instructions and 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 in R Studio 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.

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. You can run the code in two ways:
  • Line by line: select the line you want to run and use Ctrl + Enter (Windows/Linux) or Cmd + Return (Mac)
  • Run the code by clicking on the green “play” button in the top right corner of the code chunk. This will execute all the code in 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 and verify the results.

Exercise 3: Learning some R code.

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

Exercise 3: Inspect the contents of the global environment by typing and running 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.