rstudio / packrat

Packrat is a dependency management system for R
http://rstudio.github.io/packrat/
401 stars 89 forks source link

R Version Specific Packrat Cache #557

Closed adamrobinson361 closed 5 years ago

adamrobinson361 commented 5 years ago

@kevinushey - I'm trying to understand how I can set the R_PACKRAT_CACHE_DIR environment variable to be r version specific.

In my experience the packrat cache is r major version specific and you will get errors saying that a package has been built by a version of R with different internals if you just use one for multiple r versions on a system (system being widows for me).

I've been setting it dynamically in ".~/.Rprofile" but this is then overridden by the projects .Rprofile when I open projects using the .Rproj file. Are there any workarounds?

adamrobinson361 commented 5 years ago

Am I right in thinking that changing the cacheLibDir function from :

function (...) 
{
    file.path(appDataDir(), packratCacheVersion(), "library", 
        ...)
}

to

function (...) 
{
    file.path(appDataDir(), paste0(R.Version()$major, ".", R.Version()$minor), packratCacheVersion(), "library", 
        ...)
}

Would solve this problem?

If so are you happy with me pull requesting this change?

Adam

kevinushey commented 5 years ago

This would be the correct sort of change but I'm a bit wary of changing a default like this since it would effectively strip an existing Packrat cache out from underneath users by default.

That said, the fact that an existing Packrat cache can break after upgrading R is definitely bad and probably warrants a change like this.

adamrobinson361 commented 5 years ago

Agreed that when an update occurs this is a break waiting to happen suggesting a change is best.

I'm not clear the cache code so far in packrat that you can actually tell what version of R it was installed under so there could be some cashes that are already mixed. This would suggest to me that a fresh start is necessary to avoid any big changes?

I wouldn't think this would be too much of a problem for users as the cache is more of a convenience feature rather than something to rely on I would think? That said no doubt some of the cache wouldn't rebuild for some users for packages now requiring compilation and not having installed the right libraries similar to renv::equip().

adamrobinson361 commented 5 years ago

@kevinushey - just following up on this with two questions.

  1. Would you like me to put in a pull request as per the above code change for the cache or do you want me to leave it with you?
  2. Do you know if there is a workaround for this in the current set up. I considered using Renviron to set the directory so that the profile doesn't override it but can't work out if there is any way to make environment variables dynamic to specific r versions. Any advice appreciated.
kevinushey commented 5 years ago

You can have installation-specific environment variables by making use of Rprofile.site or Renviron.site. For example, the system Renviron.site would be located at:

file.path(R.home("etc"), "Renviron.site")

Note that this file doesn't exist by default; you may have to create and edit it manually.

kevinushey commented 5 years ago

Regardless, I would welcome a PR but we'd have to confirm that it doesn't regress any of the other machinery we have that currently depends on Packrat.

adamrobinson361 commented 5 years ago

Thanks Kevin - my second point was more around if you knew of if it was possible to set a dynamic environment variable r version specific using renviron vs rprofile.

e.g. PACKRAT_CACHE_DIR = C:/somedirectory/$RVersion

Thats perfectly possible in RProfile due to being able to being able to dynamically set the environment variable with sys.setenv but not sure how I'd do that without access to r code. I don't think there is an R Version environment variable.

I'll get on with submitting a pull request tomorrow,

Cheers,

Adam

kevinushey commented 5 years ago

Ah, sorry, I understand now. Unfortunately (to the best of my knowledge) there's nothing like that available.