I was trying to set up a particular Rmarkdown document in such a way that other users on our team, who might not have all the necessary packages installed, could get them automatically.
pkglist <- c("dplyr", "ggplot2", "lubridate", "qdap", "stringr", "tm", "twitteR", "wordcloud") # Extract names of any missing packages not_installed <- pkglist[!pkglist %in% installed.packages()[, "Package"]] install.packages(not_installed) # Load all the packages lapply(pkglist, library, character.only = TRUE)
The first time I tried it out it worked perfectly, but later when I re-knitted the document, this funny window popped up
I was quite surprised! Oh, what had I done now?
To cut the story short, the answer lies in the R documentation via help('install.packages')
:
So, argument pkgs
(3rd paragraph), when zero-length, is what produced the popup. After installing the missing packages in the first pass of the code I wrote, the vector not_installed
was subsequently zero-length. As an example, when one does this…
install.packages(character())
…our listbox appears.
I therefore decided to throw in a conditional statement to skip the installation step in the event that all required packages are already present:
pkglist <- c("dplyr", "ggplot2", "lubridate", "qdap", "stringr", "tm", "twitteR", "wordcloud") not_installed <- pkglist[!pkglist %in% installed.packages()[, "Package"]] # In case none of the listed packages are missing if (length(not_installed)) install.packages(not_installed) lapply(pkglist, library, character.only = TRUE)
Done.
Reblogged this on The Opportunist.
LikeLike