Working With Different Versions of an R Package

Recently, I had to install an older version of an R package, because a function was deprecated that I wanted to use. I wanted to install the old version in addition to and not instead of the new version and I came up with the following solution.

Installing Packages to a Different Location

library(devtools)
library(httr)
library(curl)

In addition to my custom Windows library, I created two new folders to store packages in, one for older packages and one for devel versions.

dev_lib <- file.path("~/../Documents/R/win-library/dev-versions/")
old_lib <- file.path("~/../Documents/R/win-library/old-versions/")

list.dirs("~/../Documents/R/win-library", recursive = FALSE)
#> [1] "C:/Users/plieninger/Documents/../Documents/R/win-library/3.5"         
#> [2] "C:/Users/plieninger/Documents/../Documents/R/win-library/dev-versions"
#> [3] "C:/Users/plieninger/Documents/../Documents/R/win-library/old-versions"

Then, I can install packages in these folders using custom arguments:

install.packages("~/../Downloads/MplusAutomation_0.7.tar.gz",
                 lib = old_lib, repos = NULL)

Installing Packages to a Different Location Using devtools

devtools::install_version() allows to install a specific version of a package automatically without having to manually download the archive. However, you cannot use a custom path, but withr::with_libpaths() can take care of that. Here, for example, I install an old version of MplusAutomation and the devel version of ggplot2.

withr::with_libpaths(new = old_lib,
                     code = install_version("MplusAutomation",
                                            version = "0.7",
                                            dependencies = FALSE))
withr::with_libpaths(new = dev_lib,
                     code = install_github("tidyverse/ggplot2",
                                           dependencies = FALSE))

Note, that I loaded devtools, httr, and curl above, because they need to be available inside with_libpaths(). Furthermore, I used dependencies = FALSE, because I already have the package dependencies in my default folder "/win.library/3.4/" and do not want them to be installed in the custom folders as well.

Loading Packages

When I load MplusAutomation (version 0.7-1), the function extractModelSummaries() is not available, and the call returns an error.

suppressPackageStartupMessages(
    library(MplusAutomation)
    )

# doesn't work:
MplusAutomation::extractModelSummaries()
#> extractModelSummaries has been deprecated. Please use readModels("nameofMplusoutfile.out", what="summaries")$summaries to replicate the old functionality.

However, when I load the old version 0.7, I can use this function as I did in previous years.

library(MplusAutomation, lib.loc = old_lib)

# works:
MplusAutomation::extractModelSummaries()

comments powered by Disqus