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

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source