1.5 Literate programming

There are two approaches to use R to perform data anaylsis:

  • The first approach is done interactively via the console window. You can tell you are in this window by the > prompt and the blinking cursor. When you are in this window, all commands are executed immediatedly as soon as you hit the [Enter] key.
  • The second approach is by writing scripts (or text file) and then running the script from top to bottom. This is done via the editor window (not visible by default, only when you open a script file.) The commands entered in this screeen is not executed immediatedly. A script consists of a collection of commands that perform a larger task on the data and is generally executed from top to bottom. A quick example will be shown in Section ??.

The preferred mode of operation is up to the user. There are pros and cons using either method. The interactive mode is quick to start and you get immediate feedback about your data. However, it does not support repeated analysis very well as you would constantly have to retype the same lines of code again and again. The scripting method is very useful for repeated analysis but can be slow to set up as mistakes in the command can become hard to debug for large scripts. With practice you will become more familary (and faster) and will develop your own style of working.

When using RStudio, it allows you a third mode of operation which is a hybrid of the two approaches mentioned above. That is, you write your commands in the editor window and run sections of the code (or chunks) as required. So the analysis is done semi-interactively.

For this workshop, we recommend that you use the script method to work through the exercises. That is, create a new text file for each section and execute the lines one at a time so you can see what happens with each command. This will allow you to come back to the exercises in your own time and review the content.

Literate programming

  1. Try some basic functions in the Console window (the large panel to the left)

    1. Type 1 + 1 and hit [ENTER], what is the output and where is it shown?
    2. Type print(“HELLO WORLD”) command in the console and hit [ENTER].
    3. Try some other functions, e.g.
    • 3^2, which is three squared
    • 11%%2, which is the modulo function (the remainder left after the first number is divided by the second).
  2. Go to File and select New File > R Script and type the same commands in the editor window.

    1. Highlight only line 1 and click on the Run button at the top right of the editor window (or hit [Ctrl]+[Enter] on the keyboard)
    2. Where is the output shown?
    3. Highlight all three lines and click on the Run or [Ctrl]+[Enter]
  3. Create a New Project that we will use for this course.

    1. Under the File menu, select New Project
    2. Click on New Directory, then Empty Project
    3. Select a name for your project. Generally this should reflect the analysis that you will be performing. Remember, you may want to come back to your projects months or even years in the future.
    4. You should now see an .Rproj file in your Files tab, with the name of the project you have just chosen. This file will contain your R session.

Below are some helpful tips when using RStudio to help you speed up your analysis.

The up arrow key

When typing in the console window, you can recall previous commands by hitting the [up arrow] key. This will cycle through the history of your previous commands starting with the most recent. Use the [up arrow] and [down arrow] keys to move through the commands. Hit the [esc] key to escape and return to the > prompt.

The history window

The history window on the top right hand corner (next to Environment) holds a history of all the commands you have executed in the past. This is a faster way of navigating your previous commands to rerun them. But this window is only available in these types of development environment software like RStudio. You even: * search your history using the search box * highlight the desired lines and click on either the To Console or To Source button