# A shortcut function for install.packages() and library()

September 10, 2011
By

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