This lesson is being piloted (Beta version)

Hello World - interacting with R

Overview

Teaching: 25 min
Exercises: 15 min
Questions
  • How to interact with R?

  • How to install packages?

Objectives
  • Install additional packages using the packages tab.

  • Install additional packages using R code.

Interacting with R

The basis of programming is that we write down instructions for the computer to follow, and then we tell the computer to follow those instructions. We write, or code, instructions in R because it is a common language that both the computer and we can understand. We call the instructions commands and we tell the computer to follow the instructions by executing (also called running) those commands.

There are two main ways of interacting with R: by using the console or by using script files (plain text files that contain your code). The console pane (in RStudio, the bottom left panel) is the place where commands written in the R language can be typed and executed immediately by the computer. It is also where the results will be shown for commands that have been executed. You can type commands directly into the console and press Enter to execute those commands, but they will be forgotten when you close the session.

Type into the console: print("Hello, World")

Because we want our code and workflow to be reproducible, it is better to type the commands we want in the script editor, and save the script. This way, there is a complete record of what we did, and anyone (including our future selves!) can easily replicate the results on their computer.

Let’s create a new script where we will save the commands that we cover in this section. Create a new script by clicking File > New File > R Script Before proceeding, be sure to save your new script File > Save As. Let’s name our script Introduction.R and save it in our scripts folder.

Type into your new script file: print("Hello, World")

RStudio allows you to execute commands directly from the script editor by using the Ctrl + Enter shortcut (on Macs, Cmd + Return will work, too). The command on the current line in the script (indicated by the cursor) or all of the commands in the currently selected text will be sent to the console and executed when you press Ctrl + Enter. You can find other keyboard shortcuts in this RStudio cheatsheet about the RStudio IDE.

Place your cursor on the line of code and try out the keyboard shortcut for your system. You will notice the command and the output appear in your console.

At some point in your analysis you may want to check the content of a variable or the structure of an object, without necessarily keeping a record of it in your script. You can type these commands and execute them directly in the console. RStudio provides the Ctrl + 1 and Ctrl + 2 shortcuts allow you to jump between the script and the console panes.

If R is ready to accept commands, the R console shows a > prompt. If it receives a command (by typing, copy-pasting or sent from the script editor using Ctrl + Enter), R will try to execute it, and when ready, will show the results and come back with a new > prompt to wait for new commands.

If R is still waiting for you to enter more data because it isn’t complete yet, the console will show a + prompt. It means that you haven’t finished entering a complete command. This is because you have not ‘closed’ a parenthesis or quotation, i.e. you don’t have the same number of left-parentheses as right-parentheses, or the same number of opening and closing quotation marks. When this happens, and you thought you finished typing your command, click inside the console window and press Esc; this will cancel the incomplete command and return you to the > prompt.

Installing additional packages using the packages tab

In addition to the core R installation there are in excess of 10,000 additional packages which can be used to extend the functionality of R. Many of these have been written by R users and have been made available in central repositories, like the one hosted at CRAN for anyone to download and install into their own R environment. In the course of this lesson we will be making use of several of these packages such as ‘ggplot2’ and ‘dplyr’.

Additional packages can be installed from the ‘packages’ tab. On the packages tab click the ‘Install’ icon and start typing the name of the package you want in the text box. As you type packages matching your starting characters will be displayed in a drop down list from where it can be selected.

Install Packages Window

At the bottom of the Install Packages window is a check box for ‘Install’ dependencies. This is ticked by default, which is usually what you want. Packages can (and do) make use of functionality built into other packages, so for the functionality contained in the package you are installing to work properly, there may be other packages which have to be installed with them. The ‘Install dependencies’ option makes sure that this happens.

Exercise

Use the install option from the packages tab to install the ‘tidyverse’ package.

Solution

From the packages tab, click ‘Install’ from the toolbar and type ‘tidyverse’ into the textbox then click ‘install’ The ‘tidyverse’ package is really a package of packages, including ‘ggplot2’ and ‘dplyr’, both of which require other packages to run correctly. All of these packages will be installed automatically. Depending on what packages have previously been installed in your R environment, the install of ‘tidyverse’ could be very quick or could take several minutes. As the install proceeds messages relating to the progress will be written to the console. You will be able to see all of the packages which are actually being installed.

Because the install process accesses the CRAN repository, you will need an Internet connection to install packages in this way.

It is also possible to install packages from other repositories as well as Github or the local file system, but we won’t be looking at these options in this lesson.

Installing additional packages using R code

If you were watching the console window when you starting the install of ‘tidyverse’ you may have noticed that before the start of the installation messages, the line

install.packages("tidyverse")

was written to the console.

You could also have installed the tidyverse packages by running this command directly at the R Console. Packages only need to be installed once on any computer, but they need to be activated every time you start a new R session.

It’s a good idea to keep track of what external packages you are using in your scripts, because it makes it much easier for others (and future you!) to replicate your analyses. Let’s add line of install code to our script and save it. In the next line, add the following:

library(tidyverse)

This code activates the tidyverse package and makes its functionality available in RStudio.

Before we move on, use the Git panel to commit and push your changes to Introduction.R. Make sure it is saved before committing!

Key Points

  • Use RStudio to write and run R programs.

  • Use install.packages() to install packages (libraries).