10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix E 378}while(x > 0) {ret = ret*xx = x−1}return(ret)There is no real savings here. In fact, neither function is a very good way to per<strong>for</strong>m thistask.E.3 <strong>Using</strong> files and a better editorIf you plan on doing any significant programming in R, then it is worthwhile tofamiliarize yourself with using external files to store your work (rather than the defaultworkspace) and using a different editor to facilitate interaction between the external filesand an R process.E.3.1 <strong>Using</strong> an external editorThe edit () function makes small changes easy, but the changes are not saved in aconvenient way <strong>for</strong> future reference. They can be saved in the workspace, but this canbecome quite cumbersome. It is often better to keep functions in files in the workingdirectory. Many functions can be kept in a single file, or each one in its own file.Commands can also be stored in a file <strong>for</strong> subsequent editing.The basic idea is to edit the file and then have its contents parsed and evaluated in Rline by line. This last step is done with the function source (). This process is repeateduntil you are satisfied.For example, you can save the hello function in a file called “hello.R" using a texteditor of your choice (e.g., Notepad) and then read the contents in using the source()function, as in> source("hello.R")Specifying the file name can be done conveniently with the file. choose () function. Moredetails on specifying a file are given in Chapter 1.Better text editorsIf you are going to be programming a lot in R, it makes sense to use an editor well suitedto the job. The default editor in Windows, Notepad, is quite primitive; the default editor(often vi) in UNIX may be too cryptic. The editor can be changed using options (), but towhich one? Good editors should do some work <strong>for</strong> you. For writing programs in R whatis desired are features such as on-the-fly code <strong>for</strong>matting, syntax highlighting, integrationwith an R process, and debugging help.There are several choices. The most advanced and powerful is a combination ofEmacs (either XEmacs, http://www.xemacs.org/, or GNU Emacs, http://www.gnu.org/);and ESS, (http://www.analytics.Washington.edu/statcomp/projects/ess/). Emacs is a text

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!