2

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
Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    What does `options("repos")` show on the console? If you then insert that into the block before `install.packages(.)`, then knit, does it show the same thing? – r2evans Nov 04 '21 at 11:42
  • 1
    BTW, if you think it's possible that `ggplot2` will be available (i.e., this is not intended to run in a vanilla/empty VM or docker image each time), you may consider replacing `install.packages(.)` with `if (!requireNamespace("ggplot2", quietly=TRUE)) install.packages("ggplot2")`: if it is available then you don't need to go through the time/effort of recompiling and/or reinstalling it. (FYI, for the difference between `library` and `require*`: https://stackoverflow.com/a/51263513/3358272, https://yihui.org/en/2014/07/library-vs-require/, and https://r-pkgs.org/namespace.html#search-path.) – r2evans Nov 04 '21 at 11:45
  • See https://community.rstudio.com/t/rmd-install-packages-best-practices/42591 The tl;dr is: Don't include a line to install packages in Rmd documents. – Phil Nov 04 '21 at 14:17

1 Answers1

1

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.

Giulio Centorame
  • 678
  • 4
  • 19