Micsellaneous concepts
Running R in different ways
There are various ways to run R:
- Via a GUI: This is what we have been doing so far. This is possible only in Windows.
- Via a command window: This is much as above except that we do not have the menu facilities. This is the main way most people use R in Linux or Mac. The worst difficulty here is not having a stop button to abort a program that has gone out of control.
- In batch mode: Here we submit a script file to R. R exits automaticlly after executing this script.
Running R from command line
If you are running R in Linux/Mac, you have to just open a terminal, type “R” at the prompt, and hit “enter”. We shall learn how to do the same thing in Windows. It is never a good idea to this in Windows, though, as the GUI version is more user firendly. First you have to know where R is installed in your system. In the Penn State labs, the path to the R executible is
C:\Program Files\R\R-2.9.0\bin\R.exe
Next you have to open a command prompt (Start > All programs > Accessories > Command prompt). Now type
C:\Program Files\R\R-2.9.0\bin\R
in it. You’ll be greeted by the familiar R prompt.
Running R in batch mode
Let us first create a script file called test.r containing the following lines. Keep the file on your desktop.
cat("It works!\n") #cat simply prints its arguments without #any embellishment.x = 1:100print(x)
Now open a command prompt and navigate to your desktop folder (which is V:\desktop in the Penn State labs!) Now type
Using multiple graphics windows
When we issue a command like plot
R checks if there is a graphics window already open. If it is, then the plot is isplayed there, else a new graphics window is created for the plot. It is possible for the user to explicitly create a new graphics window. In Windows the command is
win.graph()
In Linux one could use
x11()
Only one of the graphics windows is active at a time, and all subsequent plotting commands will direct their output to it. By default, the most recently created graphics window is the active one. However we can use the function dev.set
to activate some other graphics window.
plot(1:10) #Creates graphics window 1, and plots in it
win.graph() #Creates a new graphics window (blank)plot(1:100) #Draws in the new window
plot(1:3) #Draws again in the same new window
dev.set(2) #Depending on your setting #you may need to change 2 #to something else. The number #is given in the title bar of #the window you want to activateplot(1:5) #Plots in the activated window
String manipulation with R
As you have seen already, R is a command-based software, and the commands are all strings. Sometimes it is helpful to generate a command string programmatically, e.g., when the command is very long but has a simple structure. The functions paste
and eval
may prove handy in such a situation.
The paste
function combines strings (after converting its arguments to strings, if needed).
paste("X",1:10)paste("X",1:10,sep="")paste("X",1:10,sep="",collapse="+") #Try abbreviating 'collapse'!
This function proves particularly useful in conjunction with the names
function.
quas = read.table('SDSS_quasar.dat',head=T)names(quas)paste(names(quas),collapse="+")
With the eval
function you can execute a command. This is useful when the command is parsed from a programmatically generated string.
x = seq(-10,10,.1)comm = 'plot(x,sin(x),ty="l")'eval(parse(text=comm))
Model based clustering
The theory class talked about model based clustering (Gaussian mixtures, EM algorithm etc). There is an R package that implements these. It is not part of R by default. So we shall need to download it.
install.packages('mclust','v:\\Desktop')
The second argument is not needed if you had write-access to the default R package location in your machine. But you do not have that access in the Penn State lab machines. So we are specifying a location that we can write to. (Unfortunately, Penn State labs do not provide enough write access for installing a new package.)
You’ll be prompted to choose a repository from where to download the package. Choose something close to us (e.g., the one with PA 1 in its name).
To load the package type
library('mclust','v:\\Desktop')
The main function in this package is Mclust
.