When I am doing debugging of a complicated script, I often need to source the script repeatedly so that the RStudio break points are active. In this setting, I do not want to install packages that are already loaded (attached?), because RStudio strongly recommends restarting R whenever you try to install a package that is already loaded (attached?), and some of my code is being tested on large (circa 10-30 GB) data files for which uploading the file into R is time consuming.
Up to this moment, I have never had to distinguish between programs that are loaded and those that are attached, as I have always done both or neither. But I am now trying to write a function to avoid installing programs that are loaded (attached?), and I am trying to understand the relative implications of avoiding reinstalling only loaded packages, only attached packages, only packages that are both loaded and attached, or only packages that are either loaded or attached.
The code below is supposed to update all packages, and then install all packages taken from a vector of (perhaps) new packages, that are either uninstalled, or unattached, or both. However if a package is loaded but neither attached nor installed (if that is possible) then it will not be installed. Will that limitation cause problems under any plausible circumstances?
install.packs <- function(pks, ...){
update.packages(ask=FALSE)
uninstalled <- pks[!(pks %in% installed.packages(...)[ , 1])]
unattached <- pks[!(pks %in% (.packages(...)))]
new_pks <- unique(c(uninstalled, unattached))
install.packages(unattached, repos = "https://cloud.r-project.org/", ...)
}
While it does not entirely answer your question - You can run a script before you start debugging, for which packages are used in the script. In addition, to avoid version conflict in packages (e.g. if the script uses
ggplot2_2.0.0but you haveggplot2_3.0.0installed) you can use the checkpoint package.For example: