(This article was first published on R HEAD, and kindly contributed to R-bloggers)
I enjoy trying difference kind of R packages. Since I have more than 1 computers (1 at home, 1 at office and a laptop)
it is troublesome to check whether I have installed some new packages for each computer. Therefore i wrote a function to load and install packages at once. If the package does not exist, then the it will be downloaded from CRAN and be loaded it.
packages<-function(x, repos="http://cran.r-project.org", ...){
x <- deparse(substitute(x))
if (!require(x,character.only=TRUE)){
install.packages(pkgs=x, repos=repos, ...)
require(x,character.only=TRUE)
}
}
packages(Hmisc)
ThanksĀ richierocks for the suggestion of using deparse(substitute(x)) in the code.
richierocks’s
To leave a comment for the author, please follow the link and comment on his blog: R HEAD.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...

Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).