restore all R packages after installing a new version of R?

不打扰是莪最后的温柔 提交于 2021-02-08 08:40:22

问题


When you are doing an R update, what is the best approach to re-installing and updating all packages that were already installed on your previous R version when some of your packages are on CRAN but the rest are on github (or other sources)?

In the past, I've followed this approach:

Open old version of R (e.g. R 3.6) and make a copy of all installed packages:

installed <- as.data.frame(installed.packages())
#save a copy
write.csv(installed, 'previously_installed.csv')

Then install and open new version of R (e.g. R 4.1), read in the old package names and install (from default: CRAN):

previously_installed <- read.csv('previously_installed.csv')
package_list <- as.character(previously_installed$Package)
package_list

install.lib <- package_list[!package_list %in% installed.packages()]   
for(lib in install.lib) install.packages(lib, dependencies = TRUE)

This works really well but will only install packages that are on CRAN so all packages that are only on github won't be installed. Is there a way to automatically install these packages from github?

You could work out which packages weren't installed (e.g. remaining github packages):

git_packages_not_installed <- install.lib[!install.lib %in% installed.packages()] 

I think you need to know the authors name to install all github packages though so I'm not sure how to automate this (e.g. devtools::install_github("DeveloperName/PackageName"). I know you can supply two repository options but I'm not sure this helps or see here.

What is best practice in this situation?

thanks


回答1:


If you were only using CRAN packages, my advice would be similar to @CaptainHat's, but with one extra step: first copy all the old packages to the new location, but then call update.packages(checkBuilt = TRUE, ask = FALSE). This will update the ones that were built for an incompatible earlier version of R. (Only copy ones that aren't already there. If you copy base packages, that will break R.)

Unfortunately, that doesn't know how to update packages you installed from Github. I believe remotes::update_packages() should be able to handle those, but I've never actually tried it.




回答2:


Don't delete your old 'library' folder, and copy the contents into your new 'library' folder.

Eg. copy the contents of C:\Program Files\R\R-4.0.2\library into C:\Program Files\R\R-4.0.3\library. That's where R will look for them.



来源:https://stackoverflow.com/questions/65817504/restore-all-r-packages-after-installing-a-new-version-of-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!