If you are working on a university cluster, you will likely need to ask the system's administrators to install the packages site-wide - or else configure R to install packages to a local (user) library location.
If you haven't already set up a local library, then the easiest way seems to be to execute R
interactively, attempt to install a package, and follow the instructions when site-wide installation fails.
First, start R
and check the current library search path:
> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"
[3] "/usr/lib/R/library"
OK now let's try to install the stringr
package:
> install.packages("stringr")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("stringr") :
'lib = "/usr/local/lib/R/site-library"' is not writable
(it fails, then prompts)
Would you like to use a personal library instead? (yes/No/cancel) yes
Would you like to create a personal library
‘/home/steeldriver/R/x86_64-pc-linux-gnu-library/4.2’ to install packages into? (yes/No/cancel) yes
also installing the dependencies ‘glue’, ‘magrittr’, ‘stringi’
(after some time)
* DONE (stringr)
The downloaded source packages are in
‘/tmp/RtmpsVbbe1/downloaded_packages’
>
Now you can check that the newly-created libPath has been added:
>
> .libPaths()
[1] "/home/steeldriver/R/x86_64-pc-linux-gnu-library/4.2"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
FWIW the default user library location seems to come from a template in the /etc/R/ environment files:
$ grep R_LIBS_USER /etc/R/*
/etc/R/Renviron:R_LIBS_USER=${R_LIBS_USER:-'%U'}
/etc/R/Renviron.site:#R_LIBS_USER=${R_LIBS_USER-'~/R/@R_PLATFORM@-library/@MAJ_MIN_VERSION@'}
Once you've done that for one package, your script should run and install the rest to your new user library.