Getting started with R

This is a multi-post tutorial on how to get started with the programming environment called R. R is very useful for researchers doing data analysis as part of their work. It is a free software environment for statistical computing with an active community that contributes to its development. R is available for Windows, OS X, and Linux platforms. Here, we will cover the very basics from obtaining and installing the package, to getting started with the language, then programming with R, and finally exploring useful applications for data analysis.

Installing R

To install R, visit the project’s website at http://www.r-project.org/. In this site, navigate to the Comprehensive R Archive Network (CRAN) and select a local mirror to download the installer for your operating system. This is usually the binaries for the base distribution. As of writing, the latest version is R 4.0.0. Download the installer and just follow the instruction. For Windows, double-click the downloaded file to run the installer. Select the option to create a shortcut link in the Windows desktop to make it easier to run R.

Running R for the first time

If you created the shortcut link in your desktop, just double-click the link to run R. This will open the RGui (R graphical user interface) window with the R console as shown below.

RGui
R GUI window

R is a high-level interpreted programming language, which means that as you enter commands to the console, R will interpret and execute the command and will return any results. You do not need to compile your program to get an executable file in order to run your program. R is also case- and character-sensitive. R has also a built-in editor, the R Editor, to write R scripts that you can run. To open the editor, just click the File menu, then New script sub-menu.

RGUI with console and editor
RGui with console and editor windows

The image above shows the two main window types for programming R code and viewing the output. The console window is where you type commands directly to run immediately. This is typically for short, one-line commands. On the other hand, for several lines of codes, you will typically use the editor window where you can type a sequence of codes that performs some computations. This R script can then be executed in the console to run the codes. The advantage of writing script is that you can save it as plain text file with a .R extension, which can then be run at a later time or edited for additional functionalities.

Quitting R

To quit R, you can use the RGui’s menu items. Just select File, then Exit. It will then ask you if you want to save the workspace image. This image contains all the information held during the R session such as objects (variables) created during the session or loaded from previous session. If you want to continue working on these data at a later time, you can save the data to a file with a .RData extension. You can then load this file the next time you start R.

Alternatively, you can also use the console to quit R. Simply type q() in the console and press enter.

> q()

RGui will ask you if you want to save the workspace image. Click yes to save or no to quit without saving the workspace image.


Table of Contents

  1. Getting started with R
  2. Data types and data structures in R

You may also like...

Leave a Reply