-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-errors.qmd
158 lines (99 loc) · 5.19 KB
/
R-errors.qmd
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Troubleshooting R"
author: Wei Miao
affiliation: UCL School of Management
date: "`r (lubridate::ymd('20220929'))|> format('%a, %d %b %Y')`"
format:
html:
number-sections: true
df-print: paged
self-contained: true
page-layout: full
toc-depth: 2
code-line-numbers: false
code-copy: hover
knitr:
opts_chunk:
echo: true
warning: true
message: true
error: true
execute:
cache: true
freeze: true
---
In this page, I summarize the common issues running R and the troubleshooting tips. If you run into any R issues, please refer to this page as the first step for any solution.
## **'could not find function' Error**
This error arises when (1) an R package is not loaded properly, or (2) due to misspelling of the functions.
As you can see in the screenshot below, when we run the code, we get a [**could not find function "praise"**]{.underline} error in the console. This is because we have not loaded the package "praise" to which the `praise()` function belongs.
![](images/couldnotfindfunction.png)
We need to first load the package that contains the function we want to run using `library()` as shown below:
```{r}
library(praise)
```
and then use the function `praise()` to run it error-free.
```{r}
praise()
```
Meanwhile, if we misspell the `praise()` function, for instance, to `prais(),` this will also throw up a could not find function "prais" error.
```{r}
prais()
```
## **'object not found' error**
This error occurs when the particular object used in the code is not yet created or existing in the R environment.
In the example below we are trying to compute x plus 4. As you can see, we get an **'object 'x' not found'** error as the "x" object is not yet created and missing in our **R environment**.
```{r}
x+4
```
![](images/CleanShot%202022-09-29%20at%[email protected])
::: callout-note
## Solution {#.unnumbered}
Based on the missing object, go back to your previous codes and check why the object is missing. Did you forget to create it? Did you accidentally delete it?
:::
## Banner: "Packages XXX required but are not installed"
If you see this banner in your RStudio in the screenshot, it means RStudio detects some packages mentioned in the R Markdown file or R script but are not yet installed on your computer. So it prompts you for installation.
![](images/class5_RErrorPackageNotInstalled.png)
For instance, in this case, the error message means, `knitr` and `pacman` are found in the .Rmd file, but they are not installed, so RStudio is smart enough to prompt you to install.
::: callout-note
### Solution {.unnumbered}
Just click install button in the banner. RStudio will install the missing components.
:::
## Prompt Window: "R packages not up-to-date"
Since the R packages are being updated every day (just like our mobile apps, there could be bugs so that the developers have to update R packages to fix those bugs), sometimes, though we may have installed some packages, they are too old to run the functions. And you may see this prompted message:
![](images/class5_RErrorPackageNeedsUpdated.png)
It means, the aforementioned packages, `base64enc`, `digest`, etc. are outdated and must be updated to function.
::: callout-note
### Solution {.unnumbered}
Click `Yes` and RStudio will update all the packages for you.
:::
## Latex not found when kniting the .rmd/.qmd file.
If this is your first time to knit the PDF document, you may see an error message as below:
![](images/class5_RErrorLatexNotFound.png)
The error message has usually told us everything on how to troubleshoot (that's what an error message is for!).
In this screenshot, if you read along, you will find the cause of problem:
> \[...\] LaTex failed to compile,
because
> \[...\] No LaTeX installation detected (LaTeX is required to create PDF output).
and the **solution** is also consideratebly given in this error message:
> \[...\] You should install a LaTeX distribution for your platform: https://www.latex-project.org/get/
> If you are not sure, you may install TinyTeX in R: tinytex::install_tinytex()
So this error message tells us the solution:
::: callout-note
### Solution {.unnumbered}
#### Alternative solution 1: {.unnumbered}
Run the following command in Console
`tinytex::install_tinytex()`
in order to install tinytex, a simplied version of LaTex, on your laptop.
#### Alternative solution 2: {.unnumbered}
Install LaTex on your computer following the [R installation guide](R-installR.qmd)
:::
If you run the command in R Console, you will see that Latex is being installed
![](images/Class5_RErrorInstallTinyTex.png)
After this progress bar finishes, you will be able to knit the PDF document!
## Error: Connection Not Found
Connection Not Found error is usually caused by RStudio being unable to locate your files on your hard disk.
If you don't know how to find the path names for a file on your computer, please refer to
- this [link](https://www.sony.com/electronics/support/articles/00015251) for Windows\
- this [link](https://macpaw.com/how-to/get-file-path-mac) for Mac
## More Questions
Please leave a screenshot of error message in the MSTeams channel named "R QnA". I will keep updating this webpage as more questions come in.