Knitr: could not find function %>% error

Hi !

I am relatively new to using R, and would like to knit my outputs from my RStudio markdown to a html/pdf.

I have installed and loaded all packages I am using in the console (this includes Knitr, dplyr and magrittr), but when I press the "knit" buttom on the top left of the screen, I get the following error:

Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
In addition: Warning message:
In knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet, :
The file "Updated-survival-analysis-from-paul.rmd" should be encoded in UTF-8. Now I will try to read it with the system native encoding (which may not be correct). We will only support UTF-8 in the near future. Please see https://yihui.name/en/2018/11/biggest-regret-knitr/ for more info.

Execution halted

I have tried re-installing dplyr and magrittr, but, it still does not seem to work. I also do beleieve my file is encoded in UTF-8, and have not always gotten that error. Does anyone have any ideas, as to how to fix this?

-Ash

1 Like

Are you also loading your packages within the R Markdown document?

Knitting happens in a fresh R session, so if you have not loaded your packages in a code chunk, you'll get those errors.

Usually, you'd load your packages in a code chunk at the beginning of your document, after the YAML header. Like so:

```{r load-packages, include=FALSE}
library(dplyr)
library(magrittr)
library(knitr)
```
7 Likes

No, I am not loading my packages in the RMarkdown document :slightly_frowning_face:

Try adding the code in my reply and then knit the document. Do you get any other errors?

Hello, I have the same problem. I have loaded all the necessary libraries in a code chunk in my rmd file but this error still pops up. Any idea why this happened and how I can fix this?

Thank you in advance.

Hi, welcome!
Could you give us the detailed steps to reproduce your issue? Have in mind that libraries need to be loaded on each new R session, not just included on a code chunk, and that a code chunk can be configured to not to be run.

1 Like

Hi,
Thank you for your quick support. Turns out I have an extra pipe symbol %>% at the end of the code that I did not notice. I have finally sorted it out.

Thanks a lot and have nice day!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.