data princess data princess - 7 months ago 30
R Question

Installing R packages with bash script - responding to prompts

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 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]"

Now, normally, if I were entering this myself in R, I would get this as a response:

Installing package into β€˜/usr/hdp/’ (as β€˜lib’ is unspecified)
Warning in install.packages("[name_of_package]", repos = "http://...") :

'lib = "/usr/hdp/"' 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!

Answer Source

I figured out a way to do it.

First, create or modify .Rprofile. Add the installations and responses at the top of that file. For example:

repos = [CRAN or wherever you're sourcing from]
install.packages(package1, repos)
install.packages(package2, repos)
install.packages(package3, repos)
... # etc.

The 'y's should only be necessary for the first package, when R asks if you want to create a new library. The subsequent packages will also be dropped in there.

If you really want it to be hands-free and the script does other things after installing the R packages, you can add a quit() statement at the end, which will exit R and do whatever else you want the script to do. This might be annoying when you want to launch R later, so you'd want the script to pass around a couple different .Rprofiles.

Second, in your bash script, simply open R with R. The .Rprofile will be run immediately on startup, and the packages will be downloaded automatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download