Contents
 
Introduction
To R

Scripts and Functions

Scripts

If you have a bunch of R calls that you use repeatedly, you can put them all in a file and run them repeatedly with the source() function.

You can also edit R code in an Emacs buffer and send it to the R process running in another buffer. This section will show you how to do that.

Split the Emacs Window

First open an Emacs window by typing:
$ emacs &
Press C-x 2 to split the Emacs window into two. Click in the other window and open a new file - press C-x C-f and type in the file name, call it someplots.R. Emacs recognises files ending with .R and puts this buffer into a special mode for editing R code.

Now enter the following into the buffer:

# set 2x2 plots
par(mfrow=c(2,2))
# do the fit
sfit <- lm(skull$BaseL~skull$BaseH)
# plot the points
plot(skull$BaseH,skull$BaseL)
# add the fitted line
abline(sfit$coeff)
# title it
title('Plot and Fit')
# now do the other to plots
hist(sfit$resid)
title('Residual Distribution')
plot(sfit$fitted,sfit$resid)
title('Residual/Fitted Values')
# reset the plot to 1x1
par(mfrow=c(1,1))
Now we have to send this code to the R buffer below. First save the file (not strictly necessary, but safe) with C-x C-s and then send it to the R buffer with C-c C-b. The first time you do this Emacs will ask you for the process to load it into - this will usually be correctly suggested by Emacs, so just press return. You should then see the commands executing in the buffer and the plots appearing in the graphics window.

If you want to change anything about the data display, edit the text in the someplots.R buffer and hit C-c C-b again.

If you forget these keyboard commands, you'll see them listed in the drop-down menus under ESS - we are using ESS Eval and Eval buffer, meaning `evaluate the code in the current buffer'.

Functions

Scripts like we've just seen are fine for specific cases, but suppose we want something more general. For example, if someone came along with another data set of skull data, and we call it skull2, we would have to edit the file and replace skull with skull2 everywhere. We can achieve much more generality in our own code by writing our own functions in R.

Functions in R are objects exactly the same as all the other objects we've seen. Type the following at the R prompt to create a new function:

> foo <- function(x){sqrt(x)}
You now have an object called foo that is a function that does exactly what the built-in sqrt() function does. It is stored in your CHAPTER directory just as data objects are. You can delete it with rm(foo).

Emacs gives us a special mode for editing functions (or any other objects for that matter) in R. You used C-c C-d to dump and object out to a buffer, and C-c C-l to load it back into the R process.

For example, hit C-c C-d from the R buffer and it asks for the name of the object to edit. Type gmean and press enter. As this object does not exist, Emacs will create a blank page for the new function.

Now type in the function until it looks like this:

gmean <- function(x){
                   # geometric mean calculation
  prx <- prod(x)
  n   <- length(x)
  gm <- prx**(1/n)
  gm
}
Then do C-c C-l to load the code into the R process. This will create a new function, gmean() that will compute the geometric mean of a vector - the Nth root of the product of the N numbers.

If you type its name it will print out the code of the function (in fact any function will do this - type ls without parentheses).

The first line defines the function name and calls the first parameter x. The following lines compute the geometric mean and the last line before the last curly bracket tells the function what to return.

Try:

> gmean(1:10)
[1] 4.528729

Contents