-
Notifications
You must be signed in to change notification settings - Fork 37
/
06-errors.Rmd
48 lines (30 loc) · 2.75 KB
/
06-errors.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Deciphering Common R Errors {#errors}
For references on errors, check out the following two links by Noam Ross [here](https://github.com/noamross/zero-dependency-problems/blob/master/misc/stack-overflow-common-r-errors.md) and David Smith [here](http://blog.revolutionanalytics.com/2015/03/the-most-common-r-error-messages.html).
## `Error: could not find function`
This error usually occurs when a package has not been loaded into R via `library`, so R does not know where to find the specified function. It's a good habit to use the `library` functions on all of the packages you will be using in the top R chunk in your R Markdown file, which is usually given the chunk name `setup`.
## `Error: object not found`
This error usually occurs when your R Markdown document refers to an object that has not been defined in an R chunk at or before that chunk. You'll frequently see this when you've forgotten to copy code from your R Console sandbox back into a chunk in R Markdown.
## Misspellings
One of the most frustrating errors you can encounter in R is when you misspell the name of an object or function. R is not forgiving on this, and it won't try to automatically figure out what you are referring to. You'll usually be able to quite easily figure out that you made a typo because you'll receive an `object not found` error.
Remember that R is also case-sensitive, so if you called an object `Name` and then try to call it `name` later on without `name` being defined, you'll receive an error.
## Unmatched parenthesis
Another common error is forgetting or neglecting to finish a call to a function with a closing `)`. An example of this follows:
```{r eval=FALSE}
mean(x = c(1, 5, 10, 52)
```
```
Error in parse(text = x, srcfile = src) :
<text>:2:0: unexpected end of input
1: mean(x = c(1, 5, 10, 52)
^
Calls: <Anonymous> ... evaluate -> parse_all -> parse_all.character -> parse
Execution halted
Exited with status 1.
```
In this case, there needs to be one more parenthesis added at the end of your call to `mean`:
```{r eval=FALSE}
mean(x = c(1, 5, 10, 52))
```
## General guidelines
Try your best to not be intimidated by R errors. Oftentimes, you will find that you are able to understand what they mean by carefully reading over them. When you can't, carefully look over your R Markdown file again. You might also want to clear out all of your R environment and start at the top by running the chunks. Remember to only include what you deem your reader will need to follow your analysis.
Even people who have worked with R and programmed for years still use Google and support websites like Stack Overflow to ask for help with their R errors or when they aren't sure how to do something in R. I think you'll be pleasantly surprised at just how much support is available.