The solution indicated by Guannan Shen has one drawback that usually goes unnoticed.
When you run sudo R
in order to run install.packages()
as superuser, the directories in which you install the library end up belonging to root
user, a.k.a., the superuser.
So, next time you need to update your libraries, you will not remember that you ran sudo
, therefore leaving root
as the owner of the files and directories; that eventually causes the error when trying to move files, because no one can overwrite root
but themself.
That can be averted by running
sudo chown -R yourusername:yourusername *
in the directory lib
that contains your local libraries, replacing yourusername
by the adequated value in your installation. Then you try installing once again.