At my job, the edge node server that accesses our cluster is re-instantiated every day. This means I have to clone our repo and install a bunch of R packages every morning. I wrote a bash script (using https://github.com/eddelbuettel/littler to open R from the command line) to automate this and it works except for one hiccup.
Bash calls: r install.R
which opens an R session and calls the following command:
repos <- "http://... [our CRAN equivalent]"
install.packages("[package_name]",repos)
Now, normally, if I were entering this myself in R, I would get this as a response:
Installing package into ‘/usr/hdp/2.5.5.3-2/spark2/R/lib’ (as ‘lib’ is unspecified)
Warning in install.packages("[name_of_package]", repos = "http://...") :
'lib = "/usr/hdp/2.5.5.3-2/spark2/R/lib"' is not writable
Would you like to use a personal library instead? (y/n)
And I would say 'y,' followed by another 'y' when it suggests a library to create. However, when I do this from the bash script, it just says the library is not writable and aborts.
Two options I see here: 1) I tell it which library to write the packages in. I've tried this, and while the packages did go there, they arrived empty. Nothing was actually downloaded from the repo.
2) Somehow get the script to be able to pipe in the two affirmative responses I need in order to let R do its thing and create its own library.
Any suggestions on either of these would be much appreciated!
A different approach that works much better than what I originally posted:
In bash:
Create a specific library for installation (the same as the default R will want to create)
mkdir -p R/x86_64-pc-linux-gnu-library/3.3
Rscript --vanilla install_packages.R
In install_packages.R:
pkg_list <- # however you want to get this
for (pkg in pkg_list) {
install.packages(pkg, repos = "your_cran_mirror",
lib = "R/x86_64-pc-linux-gnu-library/3.3")
if (!require(pkg, character.only = T)) {
quit(save = "no", status = 1, runLast = FALSE)
}
}
Using this method, R will not prompt you and your script will run as intended.