# Getting Started

In order to get going with R, you need two components: GNU R itself, which runs your code; and an editor of some sort in which to write your code.

The first part is easy: if you’re using Windows or Mac OS X, click on the appropriate link here: download GNU R, and install the appropriate package (if you are using Windows, you want the “base” package; for OS X, you want the package for the latest version, named something like R-x.y.z-signed.pkg).  If you are using Linux, it is recommended to install through your package manager: on Debian and Ubuntu systems, install the ‘r-recommended’ package, which will give you everything you need.

The Windows and OS X packages of R come with a rudimentary editor that is opened when you double-click the “R” (or “R64”) icon.  This is perfectly usable for simple, one-time calculations using R, but for sophisticated projects you will want a more sophisticated editor package.

For this, I suggest using RStudio, at least to start with: it’s a decent R interface that works on Windows, Mac OS X, and Linux, and makes editing R code relatively easy.  The instructions and screenshots on this website are done using RStudio.  The decision of editor, however, is ultimately up to you: if you have some other preferred editor, by all means use it.  You can download RStudio here.

# RStudio quick intro

The first time you start up RStudio, you’ll see something like this (click to enlarge):

The left-hand side is what RStudio calls the “Console”: this is where RStudio runs R itself, and provides an interface for you to run single commands in the currently running R interface.  This is not where you want to write your actual code, but can be useful to run single commands or to try things out.

# Using the R Console

Let’s try using the R console briefly.  When writing sophisticated programs of more than a line or two, we’ll want to use RStudio’s editor, but for now, let’s experiment with the console.  Don’t worry about the R syntax shown below: we’ll get to that soon enough.  For now, just pay attention to how the R console works.

The following code is meant to assign powers of 2 from 21 to 210 (i.e. 2, 4, 8, …, 1024) into the variable ‘x’.  Type the following after the > in the console window (and press enter):

2 ^ 1:10 # Calculate powers of 2

Don’t worry about the specific syntax for now, we’ll get to that soon enough.  You should now see a list of numbers that R has calculated.  If you look closely, you’ll see the answer R gives is not what we were looking for: it answered 2 3 4 5 6 7 8 9 10, but we wanted 2 4 8 16 32 64 128 256 512 1024!  We need to fix something.

Note that in this console window you cannot actually edit previous lines: all you are allowed to do is provide a new command, and press enter.  Note, however, that you can use the up and down arrows to copy the content of previous lines, which you can then change and run again.  Try this now, to change the code we wrote into the following by adding brackets, without writing the whole line again:

2 ^ (1:10) # Calculate powers of 2

Simply press the up arrow, add the brackets in the right place, and hit enter.  You should now get the intended output.

# Create a program

Doing everything on the console makes life difficult: you have to enter commands one-by-one, which doesn’t help you when you are writing a program of several lines.

So let’s create our first standalone R program.  Click the first icon in the toolbar, and click “R Script”:

The top half of the console will now contain your new R script, and look like the following.  I’ve resized the script part of the window (by dragging the horizontal and vertical bars around the script area) to give myself more space.

This is the screen in which you’ll write most of your R programs.  Let’s get started with a first, basic program.