Getting to know R

Table of contents

Table of contents
How to use R in the microcomputer room C128
General instructions on using R
Working directory, working folder
Giving commands
Using R through an editor
Installing packages in C128
Installing R on your own computer
Updating R
Installing packages

How to use R in the microcomputer room C128

Currently (14 Jan 2011), we have the version R 2.10.1 installed in the microcomputer room. In order to use it, you need to do the following.

Start > All Programs > R > R 2.10.1

After this, the R graphical user interface Rgui should start.

hist(rnorm(100))

I.e., you should write that text in the window labeled R Console and the press the Enter key. As a result, you should get a graphics window with a histogram.

General instructions on using R

Working directory, working folder

Always remember to set the current directory or working directory (the Microsoft term for a directory is folder) before you do anything serious with R. You should make a new directory (folder) for each major project that you do with R and store your data and R code related with the project in that directory.

In the microcomputer room, you can put your own files under My Documents. First make a fresh directory, e.g., as follows. Open the windows file explorer (Start > Documents > My Documents) and create a new directory (folder) (File > New > Folder), and finally change its name to, e.g., rdir. This creates a new directory, whose path is My Documents\rdir.

You should set the working directory at the start of each of your sessions with R. If you have already started Rgui, then you can change the working directory from the menu

File > Change dir ...

It is also possible to create an icon, which starts R in automatically in some given directory. This should be possible with the following steps

After this you will have, in your working directory, a file with the name .Rdata. You can start Rgui by clicking the icon of that file, and the working directory of Rgui will be set to the directory, where the file .Rdata resides.

Giving commands

You work with R by giving commands. You can type commands directly to the R console (after the prompt, which usually is >). However, it is better to write the commands using an editor and then send the commands, e.g., one by one, for R to execute. R echos the values of arithmetic expressions, but if your command is an assignment statement, then you will see no output. For instance, take a look at this.

> 1+1
[1] 2
> a <- 3
> a
[1] 3
Here the user actually has written the text
1+1
a <- 3
a
and the rest of the output was calculated by R. The strange string [1] indicates that the result was a vector, whose first (and only) value was then printed. This seems pointless, until you see results, which span several lines. Try this.
> 1:30
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30

Usually Enter ends your command and then R evaluates it. However, if the expression remains incomplete, then the prompt of R changes to + and R waits for completion.

> 1+
+ 1
[1] 2
If you made a mistake and do not want to complete the command, press the Esc key.

Using R through an editor

It is always a good idea to write your R commands with an editor. That makes it easy to change or correct your commands, if that turns out to be necessary.

E.g., you can use the R editor, which you can launch from the Rgui menus. You can create a new script with

File > New script

or open an existing one with

File > Open script...

Inside the R editor, you can execute a block of R code by selecting it and then pressing Ctrl+R.

An alternative to the R editor is your favorite (programmer's) editor on Windows. First write your commands and the copy-paste them in the R console.

Installing packages in C128

This information needs to be updated.

Installing R on your own computer

Installing R is easy. It is avalable for various versions of Windows, Linux and MacOS X. You can find the installation programs and installation instructions on CRAN (The Comprehensive R Archive Network).

If your operating system is Windows, then installation happens as follows. Notice that the details may change as new versions of R become available.

Updating R

From time to time there will be new versions of R available. Then it is time to upgrade. If you have a Windows computer, then consult the R for Windows FAQ for advice. It is available on CRAN->FAQs (see the answer to question 2.8 What's the best way to upgrade).

Installing packages

If you have an up-to-date version of R running on your own computer, then installing packages is very easy. However, if you are using an older version of R, then not all packages are available for installation, so you might want to update R first. To install a package in Windows, first choose from Rgui

Packages > Install package(s)...

After this you select a CRAN mirror from the window that pops up, e.g., the one in Sweden. The last step is to select the name of the package you want from the window. Before you can use functions or data of the library, you must load it by giving a command of the form library(name).

Last updated 2011-01-14 16:39
Petri Koistinen
petri.koistinen 'at' helsinki.fi