Today we are going to get acquainted with R. A convenient way to work with R is through RStudio which adds many features and convenient options to the plain R interface. Most of these features go beyond the scope of this course, but as you will develop your R skillset, you might run into the need for them and hence RStudio so it is worthwhile to get to know RStudio right from the beginning.

If you have no experience with R, you will learn the most from following this document step by step. If you have some experience with R already, I suggest you try solving the questions without looking at the answers/walkthrough. You can always refer to the solutions as needed.

If you have any questions or if you feel that some code should be elaborated, feel free to ask.


Exercises

We start with the very basics and will move towards more advanced operations in R. First we will get acquainted with the language and the environment we work in.


  1. Open RStudio

The following window will appear. You might be faced with a bit different layout, but don’t dispair. Close to everything can be configured to your likings, including the colour scheme!
alt text

RStudio is divided in 3 panes, namely the console, the environment/history pane and the pane where we can access our files, plots, the help files, make packages and view our data objects. You can change the order of the panes to your liking through RStudio’s preferences. I did, that is why your pane layout might differ from the layout in the above screenshot.

If we open an R-script (i.e. a file that contains R code), a fourth pane opens.


  1. Create a new R-script

In the top left you will find alt text. Click it and select R-script. (Real hackers of course quickly learn the shortcut, Ctrl+Shift+N)

A new pane opens, and we can start typing our code. It is preferable to work from an R-script instead of directly working in the console for at least the following reasons (there are many more).

  1. You will not lose code, since RStudio caches files even if they are not saved.
  2. You log your own work. Code does not disappear over time, as you save the code in a separate file.
  3. With access to your R-script and data, others are able to exactly reproduce your work. Reproducibility is becoming increasingly more important, and this is where RStudio really excells.
  4. You become more organized in coding, as you focus on creating readable code. In the long run, this will result in you becoming a more efficient programmer. Remember: efficient code runs faster.

  1. Type the following in your new R-script
# Exercise 3
a <- 100

The # tells R that everything that follows in that specific line is not to be evaluated as code. Hence, you can use the # to create a comment in your R-scripts (or to temporarily leave out a line of code). I used # here to elaborate that the following line is the code from exercise 3 (of practical A).

The line a <- 100 assigns the value 100 to object a. When you run your code, it will be saved. The value 100 and the letter a are chosen to illustrate assigning in R. You might as well assign 123 to banana if you like. Really, anything goes.


  1. Select both lines of code and run the code by pressing Ctrl+Enter or Ctrl+R

Your code is executed and now appears in the console. If you type a in the console, R will return the assigned value. Try it! alt text

The shortcut Ctrl+Enter is your friend: it runs the current selection, or, if nothing is selected, the current line. If Ctrl+Enter yields no result, you probably have selected the console pane. You can switch to the code pane by moving the mouse cursor and clicking on the desired line in the code pane, or through Ctrl+1. Alternatively, you can move to the console through Ctrl+2.

You have now succesfully entered and executed your first code snippet in R by using RStudio.


  1. Save your code file as Practical_A.R in a folder named Practicals

You can use the standard Ctrl+S or click on the icon alt text in the code pane.

Your document is now saved. We saved it in a separate folder so that we are able to create a project out of our practicals.


  1. Create a project by clicking on alt text in the top-right corner of RStudio

Select “New Project”, click “Existing Directory” and navigate to the folder where you have just saved your code. When all is done, click on “Create Project”

You will notice in the files pane that a file Practicals.RProj has been created

The possibility to organize your work in projects is one of the benefits of using RStudio. Within a project, everything is relative to the .Rproj file. This means that if you share the folder with someone else, this someone only has to open the .Rproj file to be able to access and run all code and documents involved with this project. Again, when considering reproducability of research, working in projects is a huge advantage.


  1. Open the R-script notebook.R

Download the file (possibly to the same Practicals directory you just created) and open it in RStudio using Ctrl+O(pen) and browse to the position where you saved the file .


  1. Have a look at the code in the script (but don’t run it just yet!) and try to decipher what is going on.

If you do not understand what you are looking at, please ask for help.


  1. Compile the script as a html file

Click on the alt text icon and select html as the notebook output format.

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.


  1. Inspect the html file you have created

The notebook feature in R-Studio is very convenient; it runs and converts any R-code to a readable file where code and output are visible. There is, however, an even better format to integrate R-code with text into a single document: Markdown!


  1. Open the file markdown.Rmd

Have a look at the code in the script and make sure that you understand what is going on. If you do not understand what you are looking at, please ask someone for help.


  1. Click Knit HTML to compile the file into a html-document.

If necessary, install the required packages. Inspect the html file and compare it to the one you have created from the notebook.R file. Content should be about just the same, but the layout is nicer and offers many more possibilities.

In the rest of this course we will teach one another about R by showing our code and the resulting output in the plenary sessions. The notebook and markdown functionality in R-Studio are very convenient ways to do this. One of the reasons for using either format is that we can assess code on every machine, without the need for the packages / functionality that you used but someone else might not have. Please use one of these formats for the other practicals.


  1. If not open anymore, open your script by clicking on Practical_A.R in the files pane. Run both lines of code again and inspect the contents of the global environment by typing and running ls()

The following is returned by R

ls()
## [1] "a"

There is one object in the environment, and it is called a. If you look at the environment pane, you can quickly see the corresponding information (i.e. there is no need to type ls()) and you can see that object a contains a value. You can even see which value.

A 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.


End of Practical A. Play around with R and Rstudio if you like. Maybe create a notebook or markdown template file for the rest of the course? Or try to figure out how to change the colour scheme for RStudio (always a favorite!)