Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

Going further

489

1:f(2, -3)

2:#3: g(z)

3:#3: h(z)

4:#3: rnorm(x)

Selection: 0

> options(error=NULL)

The code first creates a series of functions. Function f() calls function g(). Function g() calls function h(). Executing f(2, 3) works fine, but f(2, -3) throws an error. Because of options(error=recover), the interactive session is immediately moved into recover mode. The function call stack is listed, and you can choose which function to examine in browser() mode.

Typing 4 moves you into the rnorm() function, where ls() lists the objects; you can see that n = -1, which isn’t allowed in rnorm(). This is clearly the problem, but to see how n became -1, you move up the stack.

Typing c returns you to the menu, and typing 3 moves you into the h(z) function, where x = -1. Typing c and 2 moves you into the g(z) function. Here both x and z are -1. Finally, moving up to the f(2, -3) function reveals that z is -1 because x = 2 and y = -3.

Note the use of print() to view the function code. This is useful when you’re debugging code that you didn’t write. Normally you can type the function name to view the code. In this example, f is a reserved word in browser mode that means “finish execution of the current loop or function”; the print() function is used explicitly to escape this special meaning.

Finally, c takes you back to the menu and 0 returns you to the normal R prompt. Alternatively, typing Q at any time returns you to the R prompt.

To learn more debugging in general and recover mode in particular, see Roger Peng’s excellent “An Introduction to the Interactive Debugging Tools in R” (http:// mng.bz/GPR6).

20.6 Going further

There are a number of excellent sources of information on advanced programming in R. The R Language Definition (http://mng.bz/U4Cm) is a good place to start. “Frames, Environments, and Scope in R and S-PLUS” by John Fox (http://mng.bz/ Kkbi) is a great article for gaining a better understanding of scope. “How R Searches and Finds Stuff,” by Suraj Gupta (http://mng.bz/2o5B), is a blog article that can help you understand just what the title implies. To learn more about efficient coding, see “FasteR! HigheR! StrongeR!—A Guide to Speeding Up R Code for Busy People,” by Noam Ross (http://mng.bz/Iq3i). Finally, R Programming for Bioinformatics (2009) by Robert Gentleman is a comprehensive yet highly accessible text for programmers that want to look under the hood. I highly recommend it for anyone who wants to become a more effective R programmer.

490

CHAPTER 20 Advanced programming

20.7 Summary

In this chapter, we’ve taken a deeper dive into the R language from a programmer’s point of view. Objects, data types, functions, environments, and scope were each described in greater detail. You learned about the S3 object-oriented approach along with its primary limitation. Finally, methods for writing efficient code and debugging troublesome programs were illustrated.

At this point you have all the tools you need to create a more complex application. In the next chapter, you'll have an opportunity to build a package from start to finish. Packages allow you to organize your programs and share them with others.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]