Skip to Content

Updating to R 3.6.0 on MacOS

Mixed emotions

Every time there is a new major update from The R Foundation (like the recent 3.6.0 release in April). I’m always happy to see the continuing progress and the combination of new features and bug fixes, but I also dread the upgrade because it means I have to address the issue of what to do about the burgeoning number of packages (libraries) I have installed.

Up until now I confess I simply have sort of “winged it”, done the upgrade and either manually thought about what packages I “really” needed or just grabbed a few essentials and then let my needs dictate whatever else I reloaded. This time I decided to get serious about the process and pay attention to not only what I was doing but documenting it and keeping a record via some amount of coding (and this post).

I’m aware that there are full-fledged package managers like packrat and checkpoint and even a package designed to manage the upgrade for you on windows, but I’m a Mac user and wanted to do things my own way and I don’t need that level of sophistication.

So I set out to do the following:

  1. Capture a list of everything I had installed under R 3.5.3 and, very importantly, as much as I could about where I got the package e.g.  CRAN or GitHub or ???
  2. Keep a copy for my own edification and potential future use.
  3. Do a clean R 3.6.0 install and not copy any library directories manually.
  4. Take a look at the list I produced in #1 above but mainly to just download and install the exact same packages if I can find them.
  5. Make the process mainly scripted and automatic and available again for the future.

Helpful background

As I was searching the web I found a few helpful posts that saved me time in building my own solution. The primary was this post on Stack Overflow. I wanted to extend the function listed there to do a little more of my work for me. Instead of just being able to generate a listing of what I had installed from GitHub I wanted to be able to determine most of the places I get packages from, which are CRAN, GitHub and R-Forge.

So let’s load tidyverse to have access to all it’s various functions and features and then build a dataframe called allmypackages with the basic information about the packages I currently have installed in R 3.5.3.

Note - I’m writing this after already upgrading so there will be a few inconsistencies in the output

  • This could just as easily be a tibble but I chose as.data.frame
  • I am deliberately removing base packages from the dataframe by filter
  • I am eliminating columns I really don’t care about with select
require(tidyverse)
allmypackages <- as.data.frame(installed.packages())
allmypackages <- allmypackages %>%
  filter(Priority != "base" | is.na(Priority)) %>%
  select(-c(Enhances:MD5sum, LinkingTo:Suggests)) %>%
  droplevels()
str(allmypackages)
## 'data.frame':    718 obs. of  8 variables:
##  $ Package         : chr  "abind" "acepack" "AER" "afe" ...
##  $ LibPath         : chr  "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" ...
##  $ Version         : chr  "1.4-5" "1.4.1" "1.2-9" "0.1-20" ...
##  $ Priority        : chr  NA NA NA NA ...
##  $ Depends         : chr  "R (>= 1.5.0)" NA "R (>= 3.0.0), car (>= 2.0-19), lmtest, sandwich (>= 2.4-0),\nsurvival (>= 2.37-5), zoo" "R (>= 2.14.0), car, lme4, pbkrtest, reshape2, stringr" ...
##  $ Imports         : chr  "methods, utils" NA "stats, Formula (>= 0.2-0)" NA ...
##  $ NeedsCompilation: chr  "no" "yes" "no" NA ...
##  $ Built           : chr  "4.0.0" "4.0.0" "4.0.0" "4.0.0" ...

A function to do the hard work

As I mentioned above the stack overflow post was a good start but I wanted more information from the function. Rather than TRUE/FALSE to is it github I would like as much information as possible about where I got the package. The package~source function will be applied to the Package column for each row of our dataframe. For example as.character(packageDescription("ggplot2")$Repository) will get back “CRAN”, and as.character(packageDescription("CHAID")$Repository) will yield “R-Forge”. For GitHub packages the result is character(0) which has a length of zero. So we’ll test with an if else clause. If we get an answer like “CRAN” we’ll just return it. If not, we’ll see if there is a GitHub repo listed with as.character(packageDescription(pkg)$GithubRepo) as well as a GitHub username as.character(packageDescription(pkg)$GithubUsername). If they exist we’ll concatenate and return. If not we’ll return “Other”. Besides being good defensive programming this may catch the package you have built for yourself as is the case for me.

package_source <- function(pkg){
  x <- as.character(packageDescription(pkg)$Repository)
  if (length(x)==0) {
    y <- as.character(packageDescription(pkg)$GithubRepo)
    z <- as.character(packageDescription(pkg)$GithubUsername)
    if (length(y)==0) {
      return("Other")
    } else {
      return(str_c("GitHub repo = ", z, "/", y))
    }
  } else {
    return(x)
  }
}
# show the first 60 as an example
head(sapply(allmypackages$Package, package_source), 60)
##                              abind                            acepack                                AER 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                                afe                               afex                          agricolae 
##                          "R-Forge"                             "CRAN"                             "CRAN" 
##                          AlgDesign                      alphavantager                             Amelia 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                           antiword                            anytime                                ape 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                                arm                                ash                            askpass 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                         assertthat                          backports                          base64enc 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                        BayesFactor                          bayesplot                         bayestestR 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                              bbmle                            BDgraph                          bdsmatrix 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                           beeswarm                          BFEffects                                 BH 
##                             "CRAN" "GitHub repo = mattansb/BFEffects"                             "CRAN" 
##                             bibtex                          bigmemory                      bigmemory.sri 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                              bindr                           bindrcpp                              binom 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                        BiocManager                                bit                              bit64 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                             bitops                            blavaan                               blob 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                           blogdown                                BMA                           bookdown 
##   "GitHub repo = rstudio/blogdown"                             "CRAN"                             "CRAN" 
##                               Boom                      BoomSpikeSlab                               boot 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                               brew                     bridgesampling                               brio 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                               brms                        Brobdingnag                              broom 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                        broom.mixed                         broomExtra                               BSDA 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                               bsts                            BWStest                                 ca 
##                             "CRAN"                             "CRAN"                             "CRAN" 
##                              Cairo                              callr                            candisc 
##                             "CRAN"                             "CRAN"                             "CRAN"

What’s in your libraries?

Now that we have the package_source function we can add a column to our data frame and do a little looking.

allmypackages$whereat <- sapply(allmypackages$Package, package_source)
str(allmypackages)
## 'data.frame':    718 obs. of  9 variables:
##  $ Package         : chr  "abind" "acepack" "AER" "afe" ...
##  $ LibPath         : chr  "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" "/Library/Frameworks/R.framework/Versions/4.0/Resources/library" ...
##  $ Version         : chr  "1.4-5" "1.4.1" "1.2-9" "0.1-20" ...
##  $ Priority        : chr  NA NA NA NA ...
##  $ Depends         : chr  "R (>= 1.5.0)" NA "R (>= 3.0.0), car (>= 2.0-19), lmtest, sandwich (>= 2.4-0),\nsurvival (>= 2.37-5), zoo" "R (>= 2.14.0), car, lme4, pbkrtest, reshape2, stringr" ...
##  $ Imports         : chr  "methods, utils" NA "stats, Formula (>= 0.2-0)" NA ...
##  $ NeedsCompilation: chr  "no" "yes" "no" NA ...
##  $ Built           : chr  "4.0.0" "4.0.0" "4.0.0" "4.0.0" ...
##  $ whereat         : chr  "CRAN" "CRAN" "CRAN" "R-Forge" ...
table(allmypackages$whereat)
## 
##                                     CRAN          GitHub repo = cjtexas/colourgen 
##                                      704                                        1 
##        GitHub repo = duncantl/RWordPress            GitHub repo = duncantl/XMLRPC 
##                                        1                                        1 
##           GitHub repo = easystats/report      GitHub repo = haleyjeppson/ggmosaic 
##                                        1                                        1 
## GitHub repo = joachim-gassen/tidycovid19          GitHub repo = leeper/slopegraph 
##                                        1                                        1 
##    GitHub repo = lorenzwalthert/stylermd         GitHub repo = mattansb/BFEffects 
##                                        1                                        1 
##  GitHub repo = PhDMeiwp/Steel.Dwass.test           GitHub repo = rstudio/blogdown 
##                                        1                                        1 
##                                    Other                                  R-Forge 
##                                        1                                        2
allmypackages %>% 
  filter(whereat == "Other") %>%
  select(Package, Version)
##                   Package Version
## CGPfunctions CGPfunctions   0.6.3

And just to be on the safe side we’ll also write a copy out as a csv file so we have it around in case we ever need to refer back.

write.csv(allmypackages, "mypackagelistMay2019.csv")

Go ahead and install R 3.6.0

At this point we have what we need, so go ahead and download and install R 3.6.0. At the end of the installation process you’ll have a pristine copy with a new library directory. When next you restart R and R Studio you’ll see a clean new version. Let’s make use of our data frame to automate most of the process of getting nice clean copies of the libraries we want.

We’ll start by getting the entire tidyverse since we need several parts and because installing it will trigger the installation of quite a few dependencies and bootstrap our work.

# post upgrade with output surpessed
install.packages("tidyverse")
library(tidyverse)

Now we have R 3.6.0 and some additional packages. Let’s see what we can do. First let’s create two dataframes, one with our old list and one with what we have right now. Then we can use anti_join to make a dataframe that lists the differences thediff. We can use filter and pull to generate a vector of just the the packages that are on CRAN we want to install.

Note – I’m faking the output rather than reinstalling all these packages on my machine so you will see packages from the tidyverse in the listing

oldpackages <- read.csv("mypackagelistMay2019.csv")
allmypackages <- as.data.frame(installed.packages())
allmypackages <- allmypackages %>%
  filter(Priority != "base" | is.na(Priority)) %>%
  select(-c(Enhances:MD5sum, LinkingTo:Suggests))
thediff <- anti_join(oldpackages,allmypackages, by = "Package")
thediff <- droplevels(thediff)
thediff %>%
  filter(whereat == "CRAN") %>%
  pull(Package) %>%
  as.character 
##  [1] "abind"          "acepack"        "AER"            "afex"           "agricolae"      "AlgDesign"     
##  [7] "alphavantager"  "Amelia"         "antiword"       "anytime"        "ape"            "arm"           
## [13] "ash"            "askpass"        "assertthat"     "backports"      "base64enc"      "BayesFactor"   
## [19] "bayesplot"      "bayestestR"     "bbmle"          "BDgraph"        "bdsmatrix"      "beeswarm"      
## [25] "BH"             "bibtex"         "bigmemory"      "bigmemory.sri"  "bindr"          "bindrcpp"      
## [31] "binom"          "BiocManager"    "bit"            "bit64"          "bitops"         "blavaan"       
## [37] "blob"           "BMA"            "bookdown"       "Boom"           "BoomSpikeSlab"  "boot"          
## [43] "brew"           "bridgesampling" "brio"           "brms"           "Brobdingnag"    "broom"         
## [49] "broom.mixed"    "broomExtra"     "BSDA"           "bsts"           "BWStest"        "ca"            
## [55] "Cairo"          "callr"          "candisc"        "captioner"      "car"            "carData"

Just do it!

Now that you have a nice automated list of everything that is a CRAN package you can give it a final look and see if there is anything else you’d like to filter out. Once you are sure the list is right one final pipe will set the process in motion.

thediff %>%
  filter(whereat == "CRAN") %>%
  pull(Package) %>%
  as.character %>%
  install.packages

Depending on the speed of your network connection and the number of packages you have that will run for a few minutes.

That takes care of our CRAN packages. What about GitHub?

thediff %>%
  filter(str_detect(whereat, "GitHub repo")) %>%
  select(Package, Version, NeedsCompilation, whereat)
##             Package    Version NeedsCompilation                                  whereat
## 1         BFEffects      0.6.1               no         GitHub repo = mattansb/BFEffects
## 2          blogdown    0.21.19               no           GitHub repo = rstudio/blogdown
## 3         colourgen      0.2.0               no          GitHub repo = cjtexas/colourgen
## 4          ggmosaic      0.3.0               no      GitHub repo = haleyjeppson/ggmosaic
## 5            report      0.1.0               no           GitHub repo = easystats/report
## 6        RWordPress      0.2-3               no        GitHub repo = duncantl/RWordPress
## 7        slopegraph     0.1.14               no          GitHub repo = leeper/slopegraph
## 8  Steel.Dwass.test      1.0.0               no  GitHub repo = PhDMeiwp/Steel.Dwass.test
## 9          stylermd 0.1.0.9000               no    GitHub repo = lorenzwalthert/stylermd
## 10      tidycovid19 0.0.0.9000               no GitHub repo = joachim-gassen/tidycovid19
## 11           XMLRPC      0.3-1               no            GitHub repo = duncantl/XMLRPC

Here’s another chance to review what you have and whether you still want need these packages. I could automate the process and once again feed the right vector to devtools::install_github() but instead I choose to handle these manually as in devtools::install_github("leeper/slopegraph").

Same with the one package I get from R-Forge…

allmypackages %>%
  filter(str_detect(whereat, "R-Forge")) %>%
  select(Package, Version, NeedsCompilation, whereat)
install.packages("CHAID", repos="http://R-Forge.R-project.org")

At the end of this process you should have a nice clean R install that has all the packages you choose to maintain as well as a detailed listing of what those are.

Done!

I hope you’ve found this useful. I am always open to comments, corrections and suggestions.

Chuck (ibecav at gmail dot com)

comments powered by Disqus