Is there a way to automate reinstalling packages and their dependencies in r?

橙三吉。 提交于 2020-08-25 23:48:32

问题


So I used installR on Rstudio to update R to version 4.0.0, and it copied the files of my packages into the library file in the R, the directory being: C:\Users\Ibrahim\Documents\R\R-4.0.0\library

Whenever I'd call on a package, for example tidytext, it would give me:

library(tidytext)
Error: package or namespace load failed for ‘tidytext’:
 package ‘tidytext’ was installed before R 4.0.0: please re-install it 

And then I'd try installing it, and it would give me:

install.packages('tidytext')
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/tidytext_0.2.4.zip'
Content type 'application/zip' length 3008780 bytes (2.9 MB)
downloaded 2.9 MB

package ‘tidytext’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Ibrahim\AppData\Local\Temp\Rtmpmo5Lza\downloaded_packages

Calling it again gives:

library(tidytext)
Error: package or namespace load failed for ‘tidytext’:
 package ‘tokenizers’ was installed before R 4.0.0: please re-install it

And I would keep installing the next dependency and recalling the package until it would finally work.

I tried to automate this with this code I found:

lib_loc <- "C:/Users/Ibrahim/Documents/R/R-4.0.0/library"
to_install <- unname(installed.packages(lib.loc = lib_loc)[, "Package"])
to_install
install.packages(pkgs = to_install)
install.packages(pkgs = to_install, dependencies=T, INSTALL_opts='--no-lock')

And that would create a bunch of .zip files to the directory: C:\Users\Ibrahim\AppData\Local\Temp\Rtmpmo5Lza\downloaded_packages

But after a while, it eventually locks, creating a folder/file called 00LOCK in the initial /library directory, and stop the process.

And I would then delete it, but when I'd run the code again, I guess maybe it redoes many of the files already done, and still eventually locks again. Am I doing anything wrong? Is there a way I can fix this? It's a real pain to do it manually. Should I just reinstall RStudio? I'm losing hope.


回答1:


Some R packages really need recompilation after upgrade of R to a newer version. This could be your case. To reinstall and update these old packages (i.e. packages built under an earlier version of R) you can try to run this code in R console:

update.packages(ask = FALSE,
                checkBuilt = TRUE)

The ask parameter prevents R from constantly asking you to confirm every update of every package, while the checkBuilt parameter is to reinstall all packages built under an earlier version of R.

For more information see the documentation or type ?update.packages in your R console in RStudio. Hope this helps!



来源:https://stackoverflow.com/questions/62117903/is-there-a-way-to-automate-reinstalling-packages-and-their-dependencies-in-r

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