Installing ggplot2 knitting error in R markdown

Viewed 216

I keep getting this error when i try to knit my document in R, after attempting to install GGplot package.

enter image description here

The error is with line 71 but there is no code on line 71. When i run the section I get this message in the console:

trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.1/ggplot2_3.3.5.tgz'
Content type 'application/x-gzip' length 4125542 bytes (3.9 MB)
==================================================
downloaded 3.9 MB

The downloaded binary packages are in
    /var/folders/km/tr6lt7kx6h71d83606qh91hc0000gn/T//Rtmp4M6La0/downloaded_packages
RStudio Community is a great place to get help: https://community.rstudio.com/c/tidyverse
1 Answers

The message you are getting is due to the fact that install.packages() requires to specify a CRAN mirror when run not interactively, e.g. install.packages("ggplot2", repos = "https://cloud.r-project.org") for the cloud mirror. If you run it from the REPL, it will just prompt you to select one from a list.

The reason it is flagging an error at that line is due to the fact that when knitting an RMarkdown document, the error messages usually refer to the chunk, not the specific lines of code. The error message refers to the first line of code of the chunk, but not necessarily where the error happens.

As Phil mentioned in the comment, it's bad practice to include install.packages() in an Rmd document. The best way is usually to insert a setup chunk at the beginning, e.g. with {r setup, include = FALSE}, which will then contain all the packages you need to load (with library() or require()), leaving the user to install them as needed. This also avoids the output from the loading package to show up in the knitted document.

If you really want to force the package installation in your document, you can use p_load() from the pacman package, but I would advise against in an Rmarkdown document anyway, since it might be hard to trace back any problem if it fails.

Related