@xhudik that is a fantastic question. Sure thing! This is the sort of thing that takes some getting used to and could perhaps be improved. Steps to reproduce:
-
Create a new project in RStudio with packrat
enabled.
-
Install BH
and then snapshot again... depending on your settings, the snapshot
might be automatic (packrat.opts auto.snapshot: TRUE
)
install.packages('BH')
packrat::snapshot()
PackratFormat: 1.4
PackratVersion: 0.4.8.1
RVersion: 3.4.2
Repos: CRAN=https://cran.rstudio.com/
Package: BH
Source: CRAN
Version: 1.65.0-1
Hash: 95f62be4d6916aae14a310a8b56a6475
Package: packrat
Source: CRAN
Version: 0.4.8-1
Hash: 6ad605ba7b4b476d84be6632393f5765
Now, if I want to force a package version, then I can edit the lock file as you mention. I usually delete the Hash:
entry entirely, since this maps back to the version that I currently have installed. It seems to work fine without doing so though. Specifically:
PackratFormat: 1.4
PackratVersion: 0.4.8.1
RVersion: 3.4.2
Repos: CRAN=https://cran.rstudio.com/
Package: BH
Source: CRAN
Version: 1.55.0-1
Hash: 95f62be4d6916aae14a310a8b56a6475
Package: packrat
Source: CRAN
Version: 0.4.8-1
Hash: 6ad605ba7b4b476d84be6632393f5765
Then, call packrat::restore()
to restore your state to that represented by your lockfile. You will get a nice little confirmation warning (this is where being able to build from source is important):
Note that packrat
's internals get in the way here, because it does another snapshot before restoring state, so I get 1.65.0-1
in my lockfile, even though 1.55.0-1
is installed. This might be a bug / feature request (and might be paired well with a set_lock_version
function or something to make this process easier.
PackratFormat: 1.4
PackratVersion: 0.4.8.1
RVersion: 3.4.2
Repos: CRAN=https://cran.rstudio.com/
Package: BH
Source: CRAN
Version: 1.65.0-1
Hash: 95f62be4d6916aae14a310a8b56a6475
Package: packrat
Source: CRAN
Version: 0.4.8-1
Hash: 6ad605ba7b4b476d84be6632393f5765
The way to remedy that is with another packrat::snapshot()
, but here we run into a note.
I typically appreciate the verbosity, but I politely tell packrat that I know what I'm doing with packrat::snapshot(ignore.stale=TRUE)
.
Now my lockfile is in the state that I expect, with a new Hash
and packages in the state that I want:
PackratFormat: 1.4
PackratVersion: 0.4.8.1
RVersion: 3.4.2
Repos: CRAN=https://cran.rstudio.com/
Package: BH
Source: CRAN
Version: 1.55.0-1
Hash: d924d63d19a9615bdcb2548b534550f6
Package: packrat
Source: CRAN
Version: 0.4.8-1
Hash: 6ad605ba7b4b476d84be6632393f5765
Some noted points:
- Per @Tazinho 's Christmas wish - it definitely is not a "just works" or "running smooth out of the box" kind of solution, but it has all the power and flexibility I want (especially with regards to installing specific commits from a git repo, archived source versions of local packages, etc.)
- The reason for the "ignore.stale" requirement is that
packrat
does not know whether I want to keep 1.55.0-1
installed or whether the 1.65.0-1
in my lockfile is what I really want. Because of the version conflict, it checks to be sure I know what I am doing before overwriting the lockfile version. The warning might prompt me to say "Oh, I forgot to packrat::restore()
! Woops!"
- One of the big pain points that happens with
packrat
is when the R session terminates in the middle of an install and packages are left in a weird state. I'm not sure if this is crazy or not, but my response has typically been to just rm -r
the folder in question and trust packrat
to rebuild my dependencies from scratch.
Hope that helps! Packrat has been a life-saver for me, and I do lots of the version-munging that you mention. It would certainly be possible to have packrat::snapshot()
be an automated part of the development submission process and packrat::restore()
be an automated part of the release process. I have been bitten by that in the past - forget to do one or another and then things break during the release: "???? I tested this! Oh! Snap. I forgot to restore my dependencies on the new system."