1 Computing Issues This short document touches on some of the basic issues that arise when running R on the Sun machines in the lab. 1.1 Start-up To open a terminal, right click on the background. To start R, type ‘R’ at the prompt. > R At this point you can begin writing commands and doing statistics with R. However, we first deal with additional computing issues. 1.2 Working Directories Your R session has a working directory. Think of the working directory as the place where any saved files will be saved. In R, you can discover where your working directory is currently located by typing > getwd() It then can be set (changed) by typing > setwd(...) where the path for your chosen directory is placed between the brackets. For example. The following changes the working directory from ‘/home/1018/ma/knaeble’ to ‘/home/1018/ma/knaeble/Rdirectory’. > getwd() [1] "/home/1018/ma/knaeble" > setwd("/home/1018/ma/knaeble/Rdirectory") Before executing the above code I first created a folder within the ‘/home/1018/ma/knaeble’ directory named ‘Rdirectory’. You can place and name your working directory wherever you’d like. When you open a terminal, it is also operating within a directory. To change the directory, for example from ‘/home/1018/ma/knaeble’ to the desktop, you can type: > cd Desktop Or, similar to what we did within R, we can change the directory from ‘/home/1018/ma/knaeble’ to ‘/home/1018/ma/knaeble/Rdirectory’ by typing: > cd Rdirectory Take note of how the syntax is slightly different and appreciate that there is a directory for the terminal, and a perhaps different directory for R. 1 1.3 Saving Graphics Now that we have some familiarity with working directories, we can learn how to save R graphics, which can later be included in written papers. Within R you can type the following, > x=rnorm(100) > hist(x) which will produce a histogram. This histogram will serve as our generic R graphic that we wish to save. If we are running R on a PC then the graphic can be saved by pointing and clicking. This however is not an option for us in the lab. We must learn to save graphics by typing a command, not within R, but within an open terminal. The following command as a whole can be thought of as analagous to ‘save as’. > xwd | convert - foo.eps The reasoning behind the syntax ‘xwd — convert’ is not as important as knowing the roles of ’-’ and ‘foo.eps’. ‘-’ signals that you will select the graphic which is to be saved manually. This will be done just after the execution of the command. Your curser will turn to a small ‘+’ symbol. Use this to click on the graphic to be saved. ‘foo.eps’ is then the name that will be given to the saved graphic. In place of ‘foo’ choose some title that is appropriate for the graphic, such as ‘xhist’ in our case. ‘eps’ stands for encapsulated post-script, a convenient file type for graphics. It is recommended that you save graphics in ‘eps’ format for this class. Upon executing the command, and after clicking on the displayed graphic, you will hear a beep, indicating that the command has been executed and that the graphic has been saved. But, where has it been saved? It has been saved to the working directory for your terminal. 1.4 Saving a Workspace Now lets say that you have executed many commands within R. You have written a lot of code that performs a statistical analysis. We call the code (as a whole) a program. Strictly speaking, the program should be written and saved in a text editor such as ‘emacs’ - R then being viewed as software that executes the program. However, since each of our labs will contain just a few lines of code, we will work exclusively within R. Having defined some objects, we might want to simply save our work. The workspace can be saved as follows. > save.image("foo.RData") Again, ‘foo’ is just a placeholder for whatever you choose to save your work as. It will be saved to your working directory. It can be accessed again, perhaps at a later date, by typing > load("foo.RData") Note that the ‘load’ function searches for the specified RData file within the current working directory. 2 1.5 Saving an R graphic from the R command line to the R working directory A straightforward set of commands can act as a perhaps superior alternative to the above ‘xwd — convert - foo.eps’ command. >pdf(’foo.pdf’) >hist(x) >dev.off() The first line names the output and specifies the file type. The second line is the command that creates the graphic. The third line provides closure. Note that one can substitute other file types: JPG, PNG, etc in place of pdf. Once the graphic is saved it can then be used within another document or printed. 1.6 Summary This document has introduced some of the main computer issues that will arise over the course of the semester. At this point you should know how to open terminals, open R, write some (very) basic code in R, work with directories, save graphics, and save and load R-workspaces. Remember, for the most part these are issues related to computing. As the semester progresses, the emphasis will be placed squarely on learning to use R to conduct statistical analyses. 3