Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R_inferno.pdf
Скачиваний:
6
Добавлен:
21.05.2015
Размер:
947.8 Кб
Скачать

8.3. DEVILS

CIRCLE 8. BELIEVING IT DOES AS INTENDED

return (2/5) * 3:9

It will return 0.4 and ignore the rest.

8.3.28return is a function (still)

return is a function and not a reserved word.

># kids, don't try this at home

>return <- function(x) 4 * x

># notice: no error

>rm(return)

8.3.29BATCH failure

Friday afternoon you start o a batch job happy in the knowledge that come Monday morning you will have the results of sixty-some hours of computation in your hands. Come Monday morning results are nowhere to be found. The job fell over after an hour because of a stray comma in your le of commands.

Results can't be guaranteed, but it is possible to at least test for that stray comma and its mates. Once you've written your le of commands, parse thele:

parse(file='batchjob.in')

If there is a syntax error in the le, then you'll get an error and a location for the ( rst) error. If there are no syntax errors, then you'll get an expression (a large expression).

8.3.30corrupted .RData

There are times when R won't start in a particular location because of a corrupted .RData le. If what you have in the .RData is important, this is bad news.

Sometimes this can be caused by a package not being attached in the R session that the le depends on. Whether or not this is the problem, you can try starting R in vanilla mode (renaming the .RData le rst is probably a good idea) and then try attaching the le.

In principle it should be possible to see what objects a .RData le holds and extract a selection of objects from it. However, I don't know of any tools to do that.

8.3.31syntax errors

Syntax errors are one of the most common problems, especially for new users. Unfortunately there is no good way to track down the problem other than

113

8.3. DEVILS

CIRCLE 8. BELIEVING IT DOES AS INTENDED

puzzling over it. The most common problems are mismatched parentheses or square brackets, and missing commas.

Using a text editor that performs syntax highlighting can eliminate a lot of the problems.

Here is a particularly nasty error:

> lseq <- seq(0, 1, 1ength=10)

Error: unexpected input in "seq(0, 1, 1en"

Hint: the end of the error message is the important location. In fact, the last letter that it prints is the rst point at which it knew something was wrong.

8.3.32general confusion

If you are getting results that are totally at odds with your expectations, look where you are stepping:

Objects may be di erent than you expect. You can use str to diagnose this possibility. (The output of str may not make much sense immediately, but a little study will reveal what it is saying.)

Functions may be di erent than you expect. Try using conflicts to diagnose this.

Pretty much the only thing left is your expectations.

Calls to browser, cat and debugger can help you eliminate ghosts, chimeras and devils. But the most powerful tool is your skepticism.

114

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