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

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

A shortcut function for install.packages() and library(): 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.

[sourcecod]
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)
   }
}
[/sourcecod]
packages(Hmisc)

Thanks richierocks for the suggestion of using deparse(substitute(x)) in the code.

richierocks’s.

TszKin Julian Chan

Disfruta probando diferentes tipos de paquetes R. Como tengo más de 1 computadoras (1 en casa, 1 en la oficina y una computadora portátil)
Es problemático comprobar si he instalado algunos paquetes nuevos para cada computadora. Por lo tanto, escribí una función para cargar e instalar paquetes a la vez. Si el paquete no existe, se descargará de CRAN y se cargará.

[sourcecod]
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)
   }
}
[/sourcecod]
packages(Hmisc)

Gracias richierocks por la sugerencia de usar deparse (sustituto (x)) en el código.

richierocks.

TszKin Julian Chan

Leave a Reply