Prof Darren Wilkinson Professor of Stochastic Modelling
School of Mathematics & Statistics
Newcastle University

Hints and Tips for R

The following are some general hints and tips for using R. You will probably want to refer to these regularly. If you are very new to R, some of them won't make sense right away, but once you've used it a little bit, you should re-read them all, to make sure you are not making life difficult for yourself! There are also some very good notes for R, which explain in detail how to use R for data analysis. For more on R, see my Introduction to R and my R links.

To run R on a Newcastle UCS Windows XP machine, select Start, Programs, Departmental Software, Maths and Stats, R, R 1.7.1 (note that this is better, but slightly different to, the old version avilable under Geography).

To get help on R functions, select Help from the R GUI window. If you know the command you want, just use "R functions (text)...", otherwise select HTML help, and look under "Packages, base".

Always remember that you can copy and paste commands and functions from Netscape or Notepad into R - there is no need to re-type everything!

To list your R objects (functions, vectors, matrices, etc.), do ls().

To view an object, simply type its name and hit return. eg. to view myfunction, simply type myfunction on a line by itself. However, it's not a good idea to try and view very large vectors or matrices, as R becomes unusable while it prints thousands of numbers onto the screen!

Since it is easy to mess up whilst typing in a function, it is better to be able to edit it as you go along. To do this, first create a blank function, and then "fix" it. eg. To create a function called "myfunction", do


and then edit it by using fix(myfunction). This provides you with a rudimentary editor for developing your function (arrow keys, cut, copy and paste all work). Select "File, Exit" when you're done. Provided you haven't messed-up, your new function will replace the old one. If the syntax was wrong, follow the instructions given to re-edit your broken function. eg. if you mess up an edit of myfunction, do


in order to re-edit it and put it right. Note however that you should only use edit if you get an error message following the use of fix. At all other times you wish to change a function, use fix.

It's possible to save your R workspace (just save the image to your home directory, and then re-load it when you next start R), but you may find it more convenient to simply copy-and-paste functions back and forth into a Notepad text file. In particular, you should save either the text of your functions or your workspace regularly, as R has been known to crash while running big simulation functions, destroying any unsaved objects.

Darren J Wilkinson Book: Stochastic Modelling for
	      Systems Biology