9 Programming 285
9.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
9.1.1 Adding and Deleting List Elements . . . . . . . . 287
9.1.2 Naming List Elements . . . . . . . . . . . . . . . 288
9.1.3 Applying the Same Function to List Elements . . 290
9.1.4 Unlisting a List . . . . . . . . . . . . . . . . . . . 294
9.1.5 Generating a List by Using split . . . . . . . . . 294
9.2 Writing Functions . . . . . . . . . . . . . . . . . . . . . . 294
9.2.1 Documenting Functions . . . . . . . . . . . . . . 297
Contents xiii
9.2.2 Scope of Variables . . . . . . . . . . . . . . . . . . 297
9.2.3 Parameters and Defaults . . . . . . . . . . . . . 298
9.2.4 Passing an Unspecified Number
of Parameters to a Function . . . . . . . . . . . . 300
9.2.5 Testing for Existence of an Argument . . . . . . . 301
9.2.6 ReturningWarnings and Errors . . . . . . . . . . 301
9.2.7 Using Function Arguments in Graphics Labels . . 302
9.3 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
9.3.1 The for Loop . . . . . . . . . . . . . . . . . . . . 303
9.3.2 The while Loop . . . . . . . . . . . . . . . . . . . 304
9.3.3 The repeat Loop . . . . . . . . . . . . . . . . . . 305
9.3.4 Vectorizing a Loop . . . . . . . . . . . . . . . . . 305
9.3.5 Large Loops . . . . . . . . . . . . . . . . . . . . . 307
9.4 Debugging: Searching for Errors . . . . . . . . . . . . . . 308
9.4.1 Syntax Errors . . . . . . . . . . . . . . . . . . . . 309
9.4.2 Invalid Arguments . . . . . . . . . . . . . . . . . 310
9.4.3 Execution or Run-Time Errors . . . . . . . . . . . 310
9.4.4 Logical Errors . . . . . . . . . . . . . . . . . . . . 311
9.5 Output Using the cat Function . . . . . . . . . . . . . . 314
9.6 The paste Function . . . . . . . . . . . . . . . . . . . . . 316
9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
9.8 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
10 Object-Oriented Programming 323
10.1 Creating Classes and Objects . . . . . . . . . . . . . . . 325
10.2 CreatingMethods . . . . . . . . . . . . . . . . . . . . . . 328
10.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 333
10.4 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
10.5 Summary and Overview . . . . . . . . . . . . . . . . . . 334
10.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
10.7 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
11 Input and Output 349
11.1 Reading S-Plus Commands from a File:
The source Function . . . . . . . . . . . . . . . . . . . . 349
11.2 Data Import/Export: Easiest Method . . . . . . . . . . . 350
11.3 Data Import/Export: General Method . . . . . . . . . . 352
11.4 Data Import/Export: Basic Method . . . . . . . . . . . . 353
11.5 Reading Data from the Terminal . . . . . . . . . . . . . 354
11.6 Editing Data . . . . . . . . . . . . . . . . . . . . . . . . . 355
11.7 Transferring Data . . . . . . . . . . . . . . . . . . . . . . 356
11.8 Recording a Session . . . . . . . . . . . . . . . . . . . . . 356
11.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
11.10 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
xiv Contents
12 Tips and Tricks 363
12.1 Useful Techniques . . . . . . . . . . . . . . . . . . . . . . 363
12.1.1 Housekeeping: Cleaning Up Directories . . . . . . 363
12.1.2 Storing and Restoring Graphical Parameters . . . 364
12.1.3 Naming of Objects . . . . . . . . . . . . . . . . . 364
12.1.4 Repeating Commands . . . . . . . . . . . . . . . 365
12.2 Programming Environment and Techniques . . . . . . . 366
12.2.1 The Process of Developing a Function . . . . . . 366
12.2.2 Setting up an Editor and Running the Code
in S-Plus . . . . . . . . . . . . . . . . . . . . . . 366
12.2.3 Treating Data Frames as Lists . . . . . . . . . . . 368
12.2.4 Working with Graph Sheets . . . . . . . . . . . . 369
12.2.5 Incorporating and Accessing C and Fortran Programs
. . . . . . . . . . . . . . . . . . . . . . . . 371
12.2.6 Batch Jobs . . . . . . . . . . . . . . . . . . . . . . 374
12.2.7 Libraries . . . . . . . . . . . . . . . . . . . . . . . 376
12.3 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
12.3.1 Creating Factors and Ordered Factors . . . . . . 378
12.3.2 Internal Representation of Factors . . . . . . . . . 380
12.3.3 Where Levels Play a Role . . . . . . . . . . . . . 381
12.3.4 Where Factors Can Lead Their Own Lives . . . . 382
12.3.5 How Factors Come Into Life . . . . . . . . . . . . 384
12.3.6 Adding and Dropping Factor Levels . . . . . . . . 385
12.4 Including Graphs in Text Processors . . . . . . . . . . . 386
12.4.1 Generating Graphs for Windows Applications . . 387
12.4.2 Generating PostScript Graphs . . . . . . . . . . . 388
12.4.3 PostScript Graphs in LATEX . . . . . . . . . . . . 389
12.4.4 If You Don’t Have a PostScript Printer . . . . . . 390
12.4.5 Greek Letters in Graphs . . . . . . . . . . . . . . 390
12.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
12.6 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
13 S-Plus Internals 401
13.1 How S-Plus Works Under UNIX . . . . . . . . . . . . . 401
13.1.1 TheWorking Chapter . . . . . . . . . . . . . . . 402
13.1.2 Customization on Start-Up and Exit . . . . . . . 402
13.2 How S-PlusWorks UnderWindows . . . . . . . . . . . 404
13.2.1 Command Line Options . . . . . . . . . . . . . . 404
13.2.2 Start-up and Exit Functions . . . . . . . . . . . . 405
13.2.3 How the ScriptWindow works . . . . . . . . . . . 406
13.3 StoringMechanism . . . . . . . . . . . . . . . . . . . . . 407
13.4 Levels of Calls . . . . . . . . . . . . . . . . . . . . . . . . 408
13.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
13.6 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Contents xv
14 Information Sources on and Around S-Plus 413
14.1 Insightful . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
14.2 S-News: Exchanging Information with Other Users . . . 414
14.3 The StatLib Server . . . . . . . . . . . . . . . . . . . . . 414
14.4 What Next? . . . . . . . . . . . . . . . . . . . . . . . . . 415
15 R 417
15.1 Development . . . . . . . . . . . . . . . . . . . . . . . . . 418
15.2 Some Similarities Between R and S . . . . . . . . . . . . 418
15.3 Some Differences Between R and S . . . . . . . . . . . . 418
15.3.1 Language . . . . . . . . . . . . . . . . . . . . . . 419
15.3.2 Libraries . . . . . . . . . . . . . . . . . . . . . . . 420
15.3.3 Trellis-Type Graphs . . . . . . . . . . . . . . . . . 420
15.3.4 Colors and Lines . . . . . . . . . . . . . . . . . . 421
15.3.5 Data Import and Export Formats . . . . . . . . . 421
15.3.6 Memory Handling . . . . . . . . . . . . . . . . . . 421
15.3.7 Mathematical Formulae in Graphs . . . . . . . . 421
15.3.8 Graphical User Interfaces . . . . . . . . . . . . . . 421
15.3.9 Start-UpMechanism . . . . . . . . . . . . . . . . 422
15.3.10Windows Integration . . . . . . . . . . . . . . . . 422
15.3.11 Support . . . . . . . . . . . . . . . . . . . . . . . 422
15.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
16 Bibliography 425
16.1 Print Bibliography . . . . . . . . . . . . . . . . . . . . . 425
16.2 On-Line Bibliography . . . . . . . . . . . . . . . . . . . . 427
16.2.1 S-Plus Related Sources . . . . . . . . . . . . . . 427
16.2.2 TEX-Related Sources . . . . . . . . . . . . . . . . 429
16.2.3 Other Sources . . . . . . . . . . . . . . . . . . . . 429
Index 431